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
Filtering by Foreign Key Model's Attribute #1023
Comments
Hiya. Nice issue. Strictly a usage question, but so well put it’s a pleasure to read. Did you look at the field_name argument to filters? This would allow you to use foo_prop as the target for your foo filter. I’d imagine that’d just work... maybe you’d need to set choices on the widget or such (but that should be handled...) |
Hi, thank you for taking the time to reply! You've set me up on the right track! I got a chance to implement this yesterday, and using the For anyone in the future who might read this issue, here's approximately how the code came out: # app/filters.py
...snip
class BarFilter(django_filters.FilterSet):
foo = django_filters.ModelMultipleChoiceFilter(
queryset=Foo.objects.all(),
field_name="foo__prop", # This lets us keep the url as "/?foo=<value>
to_field_name="prop",
widget=CheckboxSelectMultiple(),
label="Foo",
label_suffix="",
)
class Meta:
model = bar
fields = ['foo'] You can find a reference to this in the documentation @carltongibson mentioned up thread. Specifically, see the section on ModelMultipleChoice filters. Anyways, thank you so much for taking the time to help me out! |
Super. Glad you solved it. Well done! |
Great job ! Thank you ! |
Hi there!
I was working on an issue at work and wondering what the most idiomatic way of tackling it is. We have multiple
ModelMultipleChoiceFilter
filter fields for foreign keys of a model. I'm trying to figure out how to filter by a unique attribute on the foreign keys while using aCheckboxSelectMultiple
widget, without exposing primary keys in the URLs.Here's an example that (more or less) shows how I have it set up right now.
This works totally fine in the view. The filters work and filter correctly with the right widgets. However, the url has the primary key in it, so in this example
/?foo=1
, whereas I want it to read/?foo=<prop value>
, to prevent exposing my pk as well as make urls more readable.I tried modifying fields like this, because elsewhere in Django it tends to work:
This makes the url respond to
/?foo=<prop value>
as desired, but now it doesn't useCheckboxSelectMultiple
for the form. I tried making the attribute intofoo__prop
as well, but the form rendering was still incorrect.I have a feeling what I am trying to do is supported considering its almost working, but I can't seem to figure out quite how to get it. If this is outside the current supported scope, I can write my own form to handle this. Thank you very much for any help and for the awesome project!
The text was updated successfully, but these errors were encountered: