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
Queryset filtering based on other serializer properties #4426
Comments
Any suggestions? I wonder if we should consider a |
Taking advantage of the ability to filter related choices for relation fields as suggested in http://medium.com/django-rest-framework/limit-related-data-choices-with-django-rest-framework-c54e96f5815e along with providing the vehicle in the context |
@xordoquy okay so that's neat for filtering by the request/user which is another issue I run into, but the main issue I am discussing here is filtering by data/instance – i.e. I want to filter by a property of the object… which gets complicated when we could be talking about a create/update or partial update |
@tomchristie I usually end up doing variations of this:
Technically the above leaks data – or at least it used to – I see there's been changes recently about what you yield in When I do care about the leaking, I empty the queryset:
|
@tomchristie a less elegant but marginally DRYer approach I've also taken in the past… and maybe regretted, contextualises a custom field in serializers.py
I think I named this class aptly. relations.py
TL;DR what this allows you to do is:
I don't recall if this accounted for every scenario but it accounted for most – I think the edge cases were few. Validation is always applied to the field because the serializer I don't recommend this implementation for sure, it worked for me, in older DRF, but maybe it'll help inspire something better – but hopefully as convenient. |
We've been able to solve this issue in a few different ways (depending upon how DRY we needed to be with the particular serializer):
I'm not sure if any of that is helpful at all, but I agree that it would be nice if there were a standard-ish mechanism for validating related fields. |
this isn't a duplicated of #1811 ? |
@sergiomb2 yes, I think so |
@sergiomb2 I don't think so - limit_choices_to doesn't allow us to pass a filter based on other fields in the model. |
I've approached this subject before (#2292), and am interested in seeing if there's a better way around this yet in DRF.
I have a serializer that looks like this:
And the following relations apply:
Vehicle
has manyInspection
Vehicle
has manyStatus
(defines what statuses are allowed for inspections of this vehicle)Inspection
has oneStatus
I need to filter the queryset of the
InspectionSerializer
'sstatus
field so that it only allows astatus
that belongs to theVehicle
theInspection
also belongs to.In older versions of DRF, this was pretty difficult, I would filter the
queryset
in the serializer__init__
using either request data or the instance to determine thevehicle
within the context of the current request – necessary to cover all forms of create/update. I am interested in any better way to do this.The text was updated successfully, but these errors were encountered: