-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Make related fields on ModelSerializer respect limit_choices_to
#1811
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
Comments
Agreed. We should also obsolete this comment... https://github.com/tomchristie/django-rest-framework/blob/2.4.0/rest_framework/serializers.py#L826 Added the 3.0 milestone given the incoming serializer redesign. |
Any suggestions on approach or considerations? |
Can we add a kwargs to the queryset creation?
|
There's two parts to this issue. Firstly - supporting the extra keyword argument in relational fields. |
Here are the cases I see. Lemme know if this behavior seems wrong.
Number 3 is the only one that has any ambiguity to me. It seems like it could be undesirable or unexpected behavior for the queryset to be modified in this case. |
I think I'd agree with all that, yup. |
I'm hoping to take a stab at this sometime this weekend. Tom, those are
|
I opened up #1843 with my first stab at this. Tests passed locally, though I don't have all of the python versions tox wants to test against. Interested in feedback on my approach. Specifically:
|
On review it's not clear to me if we need to support |
Actually given that the Django docs state that limit_choices_to may be re-evaluated multiple times...
I'm not sure if my suggestion is valid or not. |
I'm not sure I follow your self-rebuttal about it being re-evaluated and how that applies. I think it's a convenient way to limit the available choices to a field without having to actually provide a queryset. I'm also not sure that convenience is worth the maintenance cost. In general I'm not married to it and don't have a solid enough picture of the future of serializers to make this decision myself. I think I'm +0. |
Yeah :-| whole lot of code there. |
What's the best way for us to figure this out. Would you like a second PR that supports |
Honestly, I'd say defer this until after 3.0. That won't be all that far off and we'll be in a better position to assess then. |
Works for me. I'll try to keep up with 3.0 development.
|
Milestoning to 3.0.2 as I don't think it'd be too hard to resolve this. |
Happy to do the lifting on this if you give me some direction as to how you would prefer this be implemented. |
Unless an option in the Given that, the only case where we'd need to support We could also do something nifty, like use the same It's a slightly complex bit of API, but it's then pretty nice that we can do stuff like filter related fields to only be the instances that belong to the requesting user. |
Also refs: #1985. |
Also note that serializer representations ought to include any filtering clauses. |
This is open since 2014. I just ran into the same problem. What is a definite answer on how to solve this? |
Someone needs to step in and update #1843 |
IMHO DRF should respect limit_choices_to , is not so uncommon limit a FK to is_active=True the solution provide in #3605 , worked for me i.e. override class PrimaryKeyRelatedField(RelatedField):
even more easy is :
|
* 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
) * 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
The django
ForeignKey
fields support the keyword argumentlimit_choices_to
, which when present, limits the available choices available by default when aModelForm
is created.The serializer fields that represent relationships do not respect this keyword argument. I propose they should.
If accepted, it appears the place to do this would be in
rest_framework.relations.RelatedField
in theinitialize
method. Open to alternative suggestions on where/how to implement this.The text was updated successfully, but these errors were encountered: