  import contextlib
  import sys
  from collections import OrderedDict
  from urllib import parse
  
  from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
  from django.db.models import Manager
 
</s> remove                 getattr(item, key, None) == value
 </s> add                 attrgetter(key.replace('__', '.'))(item) == value </s> remove         return getattr(obj, self.slug_field)
 </s> add         slug = self.slug_field
        if "__" in slug:
            # handling nested relationship if defined
            slug = slug.replace('__', '.')
        return attrgetter(slug)(obj)
from operator import attrgetter
  import contextlib
  import sys
  from collections import OrderedDict
from operator import attrgetter
  from urllib import parse
  
  from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
  from django.db.models import Manager
keep keep add 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