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
Add support for limit_choices_to on related fields. #1843
Closed
pipermerriam
wants to merge
1
commit into
encode:master
from
pipermerriam:piper/support_limit_choices_to
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -822,9 +822,6 @@ def get_related_field(self, model_field, related_model, to_many): | |
|
||
Note that model_field will be `None` for reverse relationships. | ||
""" | ||
# TODO: filter queryset using: | ||
# .using(db).complex_filter(self.rel.limit_choices_to) | ||
|
||
kwargs = { | ||
'queryset': related_model._default_manager, | ||
'many': to_many | ||
|
@@ -846,6 +843,9 @@ def get_related_field(self, model_field, related_model, to_many): | |
if model_field.help_text is not None: | ||
kwargs['help_text'] = model_field.help_text | ||
|
||
if model_field.rel.limit_choices_to: | ||
kwargs['limit_choices_to'] = model_field.rel.limit_choices_to | ||
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'm not clear about whether it should be |
||
|
||
return PrimaryKeyRelatedField(**kwargs) | ||
|
||
def get_field(self, model_field): | ||
|
@@ -1101,8 +1101,6 @@ def get_related_field(self, model_field, related_model, to_many): | |
""" | ||
Creates a default instance of a flat relational field. | ||
""" | ||
# TODO: filter queryset using: | ||
# .using(db).complex_filter(self.rel.limit_choices_to) | ||
kwargs = { | ||
'queryset': related_model._default_manager, | ||
'view_name': self._get_default_view_name(related_model), | ||
|
@@ -1115,6 +1113,8 @@ def get_related_field(self, model_field, related_model, to_many): | |
kwargs['help_text'] = model_field.help_text | ||
if model_field.verbose_name is not None: | ||
kwargs['label'] = model_field.verbose_name | ||
if model_field.rel.limit_choices_to: | ||
kwargs['limit_choices_to'] = model_field.rel.limit_choices_to | ||
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. Same as previous comment. |
||
|
||
if self.opts.lookup_field: | ||
kwargs['lookup_field'] = self.opts.lookup_field | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm not sure what this code block is for.
self.limit_choices_to
is only relevant whenqueryset is not None
so why do we need to set it to anything 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.
In the event that the model field is defined as:
it seemed appropriate for DRF to respect that value and filter the default queryset.
Do you think it would be better to only do filtering when
limit_choices_to
is explicitly declared on the serializer field?