I’m working on my first Django project and trying to implement the Extra Chained Django-Select2 feature. Although both the site and asset fields render correctly with search functionality, clicking either field only generates a request like:
http://[IP]:[PORT]/select2/fields/auto.json?field_id=IjhkM2ZjY[…etc…]RFdIikUCQo
with no sign of the expected site=x
parameter. I verified the backend by manually testing a URL such as auto.json?site=5&field_id=[etc…]
, which returns the correctly filtered JSON. I’ve reviewed StackOverflow, the simpleisbetterthancomplex documentation, django-select2 Extra docs on “chain” and “dependent” dropdown topics, as well as ChatGPT suggestions, but I’m still missing the key step required for chaining.
Below is my code:
# forms.py
class ProjectForm(forms.Form):
site = forms.ModelChoiceField(
queryset=Site.objects.all().order_by('name'),
widget=ModelSelect2Widget(
model=Site,
search_fields=['name__icontains'],
attrs={ 'data-placeholder': "Pick site A", 'style': 'width: 100%;', 'data-minimum-input-length': 0 }
)
)
asset = forms.ModelChoiceField(
queryset=Asset.objects.all().order_by('name'),
widget=ModelSelect2Widget(
model=Asset,
search_fields=['name__icontains'],
dependent_fields={'site': 'site'}, <=============== not effect whatsoever it seems
max_results=500,
attrs={ 'data-placeholder': "Pick asset A", 'style': 'width: 100%;', 'data-minimum-input-length': 0 }
)
)
# models.py
class Site(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Asset(models.Model):
name = models.CharField(max_length=255)
site = models.ForeignKey('Site', related_name="assets", on_delete=models.CASCADE)
def __str__(self):
return self.name
# urls.py
urlpatterns = [
path('', views.frontpage, name='frontpage'),
path('asset/', views.turbine, name='asset'),
path('select2/', include('django_select2.urls'))
]
# views.py
def asset(request):
form = ProjectForm()
return render(request, 'asset.html', {'form': form})
<!-- asset.html -->
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>title</title>
{% load static %}
<link rel="stylesheet" href="{% static 'css/layout.css' %}">
<link rel="stylesheet" href="{% static 'css/main.css' %}">
<link rel="stylesheet" href="{% static 'css/events_white.css' %}">
<link rel="stylesheet" href="{% static 'css/colors.css' %}">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico' %}">
{{ form.media.css }}
</head>
<body>
<script var data_dir = "{% static 'data' %}"></script>
<script src='{% static "js/d3.v7.min.js" %}'></script>
<script src='{% static "js/jquery-3.7.1.min.js" %}'></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script src='{% static "js/dimensions.js" %}'></script>
<script src='{% static "js/layout_helpers.js" %}'></script>
<script src='{% static "js/layout_ganttdatahelpers.js" %}'></script>
<script src='{% static "js/layout_updategraphs.js" %}'></script>
<script src='{% static "js/layout_mousevents.js" %}'></script>
<script src='{% static "js/make_chart.js" %}'></script>
[more divs]
{{ form }}
{{ form.media.js }}
</body>
</html>
Even though the forms and dropdowns are visible and functional for search, the auto.json request never includes the dependent field parameter (site
). I don’t see any errors in the browser console or Django logs.
I’m looking for advice on what could be causing the omission and how to properly debug this issue. Any help or clarification would be greatly appreciated!