I use django-filter
for filtering a Article
queryset. Article
has a field locations
:
locations = models.ManyToManyField('locations.Location', related_name='articles')
I filter Articles
by field locations
and as a widget, I use django-autocomplete-light
widget Select2Multiple
.
class ArticleFilter(django_filters.FilterSet):
from locations.models import Location
...
locations = django_filters.ModelMultipleChoiceFilter(method='locations_filter',queryset=Location.objects.all(),label='Lokality')
...
class Meta:
model = Article
fields = ['locations','title','price_lte','price_gte','price_currency'
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# HERE I SPECIFY THE DAL WIDGET
self.form.fields['locations'].widget = autocomplete.Select2Multiple(url="locations:locations_autocomplete")
The filtering works good, autocomplete too:
When I click on submit, it shows filtered results correctly, URL querystring contains all filtered attributes including for exmaple locations=45
but it doesn't show the location
with id=45
as an initial value for the locations
field.
The weird thing is that if I remove the widget, I can see that originally, there is initial value:
Just Select2
doesn't show it and the value of locations is undefinfed
.
I use this same approach on raw django.forms
and it works correctly. Initials too.
Do you know where can be the problem?