Hello,
First post here, first django project, kindly let me know if anything seems inappropriate in my message.
Note: I went through all I could find on Stackoverflow and this forum’s links to simpleisbetterthancomplex and django-select2 Extra doc, “chain” and “dependent” dropdown topics, as well as through chatgpt best guesses.
Context : Trying to implement the Extra Chained Django-Select2 feature.
My question:
In your experience, for what kind of reason(s) would the following code never trigger a “site=x” parameter in the request to auto.json ? auto.json only fires the field_id parameter. How could I debug this ?
Note2 : The models.py are copied from the doc example and the backend/cache works (Postgre, Redis), when I manually test an “auto.json?site=5&field_id=[etc…]” URL the returned json contains the appropriately filtered data, in correct format.
[IP][:PORT]/select2/fields/auto.json?site=5&field_id=IjhkM2Zj [etc] YWI5LT
returns the correctly filtered subset :
{"results": [{"id": 7, "text": "DOUD-T001"}, {"id": 8, "text": "DOUD-T002"}], "more": false}
#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 }}
Both fields display correctly, with effective search field. However left-click on any of the two fields will only trigger:
http://[IP]:[PORT]/select2/fields/auto.json?field_id=IjhkM2ZjY[…etc…]RFdIikUCQo
No errors in the browser console, nor in the Django’s terminal window (tty).
…and the fields are not chained.
any help/clarification with this will be appreciated, I am certainly missing a key step/concept here