          except (TypeError, ValueError):
              self.fail('invalid')
  
      def to_representation(self, obj):
          return getattr(obj, self.slug_field)
  
  
  class ManyRelatedField(Field):
      """
      Relationships with `many=True` transparently get coerced into instead being
 
</s> remove                 getattr(item, key, None) == value
 </s> add                 attrgetter(key.replace('__', '.'))(item) == value
        slug = self.slug_field
        if "__" in slug:
            # handling nested relationship if defined
            slug = slug.replace('__', '.')
        return attrgetter(slug)(obj)
          except (TypeError, ValueError):
              self.fail('invalid')
  
      def to_representation(self, obj):
        slug = self.slug_field
        if "__" in slug:
            # handling nested relationship if defined
            slug = slug.replace('__', '.')
        return attrgetter(slug)(obj)
  
  
  class ManyRelatedField(Field):
      """
      Relationships with `many=True` transparently get coerced into instead being
keep keep keep keep replace keep keep keep keep keep
 Handle Nested Relation in SlugRelatedField when many=False (#8922)

* Update relations.py

Currently if you define the slug field as a nested relationship in a `SlugRelatedField` while many=False, it will cause an attribute error. For example:

For this code:
```
class SomeSerializer(serializers.ModelSerializer):
    some_field= serializers.SlugRelatedField(queryset=SomeClass.objects.all(), slug_field="foo__bar")
```
The POST request (or save operation) should work just fine, but if you use GET, then it will fail with Attribute error:

> AttributeError: 'SomeClass' object has no attribute 'foo__bar'

Thus I am handling nested relation here. Reference: https://stackoverflow.com/questions/75878103/drf-attributeerror-when-trying-to-creating-a-instance-with-slugrelatedfield-and/75882424#75882424

* Fixed test cases

* code comment changes related to slugrelatedfield

* changes based on pre-commit and removed comma which was added accidentally

* fixed primary keys of the mock object

* added more test cases based on review

---------

Co-authored-by: Arnab Shil <arnab.shil@thermofisher.com> 
rest_framework/relations.py