# Select2 Django unchained

**URL:** <https://forum.djangoproject.com/t/select2-django-unchained/38567>\
**Category:** Forms & APIs\
**Created:** [February 4, 2025, 8:55pm UTC](https://forum.djangoproject.com/t/select2-django-unchained/38567 "2025-02-04T20:55:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarcabane](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sarcabane/32/26721_2.png) [@sarcabane](https://forum.djangoproject.com/u/sarcabane)\
**Post date:** [February 4, 2025, 8:55pm UTC](https://forum.djangoproject.com/t/select2-django-unchained/38567/1 "2025-02-04T20:55:27Z")

</div>

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.

```auto
[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}

```

```auto
#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,
            }
        )
    )

```

```auto
#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

```

```auto
#urls.py
urlpatterns = [
    path('', views.frontpage, name='frontpage'),
    path('asset/', views.turbine, name='asset'),
    path('select2/', include('django_select2.urls'))
] 

```

```auto
# views.py
def asset(request):
    form = ProjectForm()
    return render(request, 'asset.html', {'form': form})

```

```auto
#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

---

<div class="post-metadata">

**Author:** ![sarcabane](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sarcabane/32/26721_2.png) [@sarcabane](https://forum.djangoproject.com/u/sarcabane)\
**Post date:** [February 6, 2025, 7:05pm UTC](https://forum.djangoproject.com/t/select2-django-unchained/38567/2 "2025-02-06T19:05:58Z")

</div>

Solution :  
If you can, in the template use “{{form}}” instead of just “{{form}}”

Cause : in django\_select2.js the function looks for the dependent\_field name within the same “form”, but the form tags were never created in the first place.
