[Fixed]-Django smart-selects ajax configuration

1👍

smart selects has it’s own form field types. You are overriding that for category right now in your form:

## Pre-populated dropdown menu
category = forms.ModelChoiceField(
    queryset=Category.objects.all(),
    label = "Category"
)

Remove all the code above from your form. When you are using a ChainedForeignKey, the model field’s default form field type is provided by smart selects (see ChainedModelChoiceField in the source).

The Javascript that performs AJAX calls is a small inline script that is rendered as part of the form field supplied by smart selects.

You don’t have to replace the code with anything, as the field is rendered automatically when included in a ModelForm.

0👍

Ian Price’s reaction works just fine. I have tried it and its pretty working. The issue is with the loading of jQuery library. It must be the first one on top of any tags to be loaded

Base.html

<head>
    <title>testing</title>
    {% load static %}
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
 <script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script>
<script src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script>
</head>
👤dungu

Leave a comment