question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModelMultipleChoiceFilter - wrong field format in request url

See original GitHub issue

Hi,

I am using django-filters ModelMultipleChoiceFilter. I am running into a problem where values from the input field are not correctly converted to the django-filters format, i.e. filter_field=value_1&filter_field=value_2&.... The value from the input field is treated as one value (none of the separators seem to work, the format of query params in request is like filter_field=value_1,value_2,...).

The filter that I have looks something like this:

from django_filters import rest_framework as filters
from django.contrib.auth.models import User

class TeamFilterSet(filters.FilterSet):
    members = filters.ModelMultipleChoiceFilter(
        label=_('Filter by member IDs'),
        field_name='members',
        queryset=User.objects.all(),
    )

Is it possible to obtain the expected format in the request url or do I have to override the actual filter to get the proper values from the request?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lderleckicommented, Jun 23, 2021

Yes, this solution works! Thank you so much for your help!

2reactions
tfranzelcommented, Jun 23, 2021

ahh now i get it! the schema you receive comes from django-filter itself. they generate a filter schema themselves but that is pretty much useless almost all of the time because they don’t have the introspection tooling we have.

our extension does not active as it is a different class. we can match subclasses but it is safer not to by default when you do custom stuff.

simply define a new extension based on the existing one. this should do it for you

from drf_spectacular.contrib.django_filters import DjangoFilterExtension

class CustomDjangoFilterExtension(DjangoFilterExtension):
   target_class = 'django_filters.rest_framework.DjangoFilterBackend'
   priority = 1
   match_subclasses = True

edit fyi: https://drf-spectacular.readthedocs.io/en/latest/faq.html#where-should-i-put-my-extensions-my-extensions-are-not-detected

Read more comments on GitHub >

github_iconTop Results From Across the Web

DRF ModelMultipleChoiceFilter - invalid value in one of many ...
I looked a bit at the django_filters code and it seems like there's no configuration/option to make the ModelMultipleChoiceFilter field do ...
Read more >
Filter Reference — django-filter 22.1 documentation
RangeWidget – this widget is used with RangeFilter to generate two form input elements using a single field. ModelChoiceFilter and ModelMultipleChoiceFilter ...
Read more >
django-filter Documentation - Read the Docs
Filters no longer autogenerated when Meta.fields is not specified . ... We need a URL pattern to call the view:.
Read more >
django-filter Documentation - Yumpu
ModelChoiceFilter, the queryset argument has callable behavior. To use a custom field name for the lookup, you can use to_field_name: class FooFilter( ...
Read more >
How do I use Django-filter with multiple values on the ... - Quora
It seems like an odd request to me, but let's go with it. Starting off, filtering a model based on a field is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found