-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Fixed #3674 -- Refactored _get_reverse_relationships() to use correct to_field. #3852
Changes from all commits
ce98c3d
21839e4
dae5a5c
b7b6ba3
15ec87c
e670284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1131,9 +1131,14 @@ def build_relational_field(self, field_name, relation_info): | |
field_kwargs = get_relation_kwargs(field_name, relation_info) | ||
|
||
to_field = field_kwargs.pop('to_field', None) | ||
if to_field and not relation_info.related_model._meta.get_field(to_field).primary_key: | ||
field_kwargs['slug_field'] = to_field | ||
field_class = self.serializer_related_to_field | ||
if relation_info.reverse: | ||
if to_field and not relation_info.related_model_field.related_fields[0][1].primary_key: | ||
field_kwargs['slug_field'] = to_field | ||
field_class = self.serializer_related_to_field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests don't appear to hit this branch, so hard to verify on first sight - some more digging may be required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it looks like we need to add a test case where the |
||
else: | ||
if to_field and not relation_info.related_model._meta.get_field(to_field).primary_key: | ||
field_kwargs['slug_field'] = to_field | ||
field_class = self.serializer_related_to_field | ||
|
||
# `view_name` is only valid for hyperlinked relationships. | ||
if not issubclass(field_class, HyperlinkedRelatedField): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea what the
[0][1]
refers to here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will need to go back in and check what the actual values are. I really should have put a comment in there when I made the change. I'm pretty sure it's grabbing the
to_field
value for the related field (see the relevant django code here) but you are totally right, it is very hard to decipher just by looking at it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
related_fields[0]
is the first field in the model'sfrom_fields
list; it's a tuple of(from_field, to_field)
so[1]
here is referring toto_field