dependent_fields does not work
See original GitHub issueHi, First of all thank you for this wonderful Django plugin. I need to create a Chained Model Multiple Select2 , and I have done everything that the documentation asked me to do regarding that. My form looks like this ->
def __init__(self, *args, **kwargs):
super(NationalFilter, self).__init__(*args, **kwargs)
# self.fields['state'].choices = getNationalStates()
# self.fields['district'].choices = getNationalDistricts()
state = ModelChoiceField(
queryset = State.objects.all(),
label = u'State',
widget = ModelSelect2MultipleWidget(
model = State,
search_fields = ['state__icontains']
)
)
district = ModelChoiceField(
queryset = District.objects.all(),
label = u'District',
widget = ModelSelect2MultipleWidget(
model = District,
search_fields = ['district__icontains'],
dependent_fields = {'state' : 'state'},
max_results = 100,
)
)
My models look like this ->
class State(models.Model):
state = models.CharField(max_length = 1000)
def __unicode__(self):
return '%s' % (str(self.state))
class District(models.Model):
state = models.ForeignKey(State)
district = models.CharField(max_length = 1000)
def __unicode__(self):
return '%s' % (str(self.district))
I call the form fields inside templates using {{ nationalFilter.state }}
and {{ nationalFilter.district }}
If I were to retain the commented code then they would work like any unchained fields do but when I remove them it says that the ‘Results could not be loaded’. I am trying to render form in the modal. The error it gives on the django command line server is the following
Not Found: /select2/fields/auto.json
[09/Aug/2017 20:25:59] "GET /select2/fields/auto.json?term=&field_id=MTM5OTk5OTQ0NTg2NjQw%3A1dfSNx%3AvpS6GwaTyvV6gXAY-06cQSJyEFg HTTP/1.1" 404 1828
The error to me seems to be related to the url configuration. So I am adding that too here. My main urls.py(the one created by default) looks like this ->
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('data_analyser.urls')),
url(r'^select2/', include('django_select2.urls')),
url(r'^chaining/', include('smart_selects.urls')),
]
The other one that I created for my app looks like this ->
urlpatterns = [
url(r'^main$', views.main, name = 'main'),
url(r'^select2/', include('django_select2.urls')),
]
Could you guys tell If I am doing anything wrong.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (4 by maintainers)
Thanks @codingjoe
I implemented the Multi-Select Dependency widget as below, if someone needs it
I am essentially using “[]” suffix at the end of request params to identify variables sending a list. Also, had to use direct “filter” method instead of a “Q” method in the queryset.
For the #TODO I did: if dependent_fields_multi_value: for key,val in dependent_fields_multi_value.items(): select &= Q(**{“%s__in” % key: val})