-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Respect to_field property of ForeignKey relations #2435
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
Changes from 1 commit
067360c
8263048
d47f29a
49ecaa7
6369f93
6007463
f808b6c
77eeefa
3dd5ddd
6ccc45e
64c49f8
a496c52
8096997
54e16f4
5ca9841
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Uses the SlugRelatedField instead. SlugRelatedField will only get the Slugfield from the ORM as it does not need the other stuff
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -727,6 +727,7 @@ class ModelSerializer(Serializer): | |
| models.URLField: URLField, | ||
| }) | ||
| _related_class = PrimaryKeyRelatedField | ||
| _related_to_field_class = SlugRelatedField | ||
|
|
||
| def create(self, validated_data): | ||
| """ | ||
|
|
@@ -1002,8 +1003,15 @@ def get_fields(self): | |
| field_cls = self._get_nested_class(depth, relation_info) | ||
| kwargs = get_nested_relation_kwargs(relation_info) | ||
| else: | ||
| field_cls = self._related_class | ||
| kwargs = get_relation_kwargs(field_name, relation_info) | ||
| to_field = kwargs.get('to_field', False) | ||
|
Contributor
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. Can you just do |
||
| if to_field: | ||
| # using the slug field for now | ||
| kwargs.pop('to_field', None) | ||
|
Contributor
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. As we are popping this just a few lines above, the line shouldn't be needed. |
||
| kwargs['slug_field'] = to_field | ||
| field_cls = self._related_to_field_class | ||
| else: | ||
| field_cls = self._related_class | ||
| # `view_name` is only valid for hyperlinked relationships. | ||
| if not issubclass(field_cls, HyperlinkedRelatedField): | ||
| kwargs.pop('view_name', None) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,7 +194,7 @@ def get_relation_kwargs(field_name, relation_info): | |
| """ | ||
| Creates a default instance of a flat relational field. | ||
| """ | ||
| model_field, related_model, to_many, has_through_model = relation_info | ||
| model_field, related_model, to_many, to_field, has_through_model = relation_info | ||
| kwargs = { | ||
| 'queryset': related_model._default_manager, | ||
| 'view_name': get_detail_view_name(related_model) | ||
|
|
@@ -203,6 +203,9 @@ def get_relation_kwargs(field_name, relation_info): | |
| if to_many: | ||
| kwargs['many'] = True | ||
|
|
||
| if to_field: | ||
| kwargs['to_field'] = to_field | ||
|
Contributor
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. Should this be
Contributor
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. It's being used in
Contributor
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.
Sorry I didn't understand - you might need to link me to that?
Contributor
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. https://github.com/tomchristie/django-rest-framework/pull/2435/files#diff-80f43677c94659fbd60c8687cce68eafR1007 and the line with your comment below it.
Author
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. I named it to_field because mentally i am still treating it like a ForeignKey object at that point. Pointing it to a slug_field afterwards was just handy |
||
|
|
||
| if has_through_model: | ||
| kwargs['read_only'] = True | ||
| kwargs.pop('queryset', None) | ||
|
|
||
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.
Pretty sure the
onlyoptimization is unrelated and should be removed.