Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Nested Relation in SlugRelatedField when many=False #8922

Merged
merged 6 commits into from Apr 8, 2023

Conversation

ruddra
Copy link
Contributor

@ruddra ruddra commented Mar 30, 2023

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. Apart from that, I have added test cases regarding nested relations.

Reference: https://stackoverflow.com/questions/75878103/drf-attributeerror-when-trying-to-creating-a-instance-with-slugrelatedfield-and/75882424

ruddra and others added 4 commits March 30, 2023 01:18
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
@ruddra ruddra changed the title Patch 1 Handle Nested Relation in SlugRelatedField when many=False Mar 30, 2023
@auvipy auvipy self-requested a review March 30, 2023 05:46
@ruddra
Copy link
Contributor Author

ruddra commented Apr 3, 2023

Hi @auvipy, is there any update regarding this PR, please?

tests/test_relations.py Show resolved Hide resolved
@ruddra ruddra requested a review from auvipy April 5, 2023 09:12
@ruddra
Copy link
Contributor Author

ruddra commented Apr 7, 2023

@auvipy Hi, can you please review this PR? I have added the necessary changes as you have asked.

@auvipy
Copy link
Member

auvipy commented Apr 8, 2023

sure

@auvipy auvipy added the Bug label Apr 8, 2023
@auvipy auvipy added this to the 3.15 milestone Apr 8, 2023
@auvipy auvipy merged commit 959085c into encode:master Apr 8, 2023
7 checks passed
@ruddra ruddra deleted the patch-1 branch April 8, 2023 09:46
@ruddra ruddra restored the patch-1 branch April 8, 2023 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants