-
-
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
Fix #1811: take limit_choices_to into account with FK #6371
Conversation
Finally I took some time to add a simple test for this. |
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.
Would it be possible to move the test within the test_relations_pk.py
file ?
I moved my tests to |
Thanks for this. Looks like it's working towards a resolution. |
What better feeling than fixing a 5-year old issue ? 😁 What do you think about the latest changes ? |
@@ -266,6 +266,9 @@ def get_relation_kwargs(field_name, relation_info): | |||
# If this field is read-only, then return early. | |||
# No further keyword arguments are valid. | |||
return kwargs | |||
limit_choices_to = model_field.get_limit_choices_to() | |||
if limit_choices_to: | |||
kwargs['queryset'] = kwargs['queryset'].filter(**limit_choices_to) |
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 think this block should probably go above the if has_through_model:
switch. (L252) Since there's a couple of cases where the queryset
keyword argument gets popped off.
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.
Or else use if queryset in kwargs and limit_choices_to:
, which would also do the trick.
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.
You're right. I did not think it through. I just wanted to avoid extra work putting it after the read_only but forgot that queryset
can be popped.
Maybe another solution would be to add a check like 'queryset' in kwargs
?
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 was too slow to post my comment and did not see yours. :/
Nice one! I think there's one last bit to fix here - making sure that the change still works in cases where the |
I'm switching back my solution to the previous one and adding the queryset in kwargs check. Would you like a rebase to avoid all this back and forth commits ? |
@adrianmester No need to switch it - looks good as is. And no to the rebase - I'll just use Squash and merge. |
Neat, yup. It's the oldest open issue on our list! https://github.com/encode/django-rest-framework/issues?page=6&q=is%3Aissue+is%3Aopen |
Allright. That's good for me. It was a pleasure to be part of the team for the last half hour or so 😃 |
Boom 💥 |
It's possible that (boom is denied 😄 ) |
A different version, which consider Q objects: dogukantufekci@607059d |
) * Fix issue1811: take limit_choices_to into account with FK * Issue 1811: Add tests to illustrate issue * Filter queryset only if limit_choices_to exists * Move test_relations_with_limited_querysets file within test_relations_pk * move limit_choices_to logic from relations.py to utils/field_mapping.py * move limit_choices_to above other check to avoid conflicts
Closes #1811
I created a small repo to test this fix and it works as expected. It is based on #5358 proposal. (I've done nothing other than formatting the code)
Tests are passing but some more tests for this particular bit should be added, hence the WIP in the title.
(If anyone wants to help...)