3

I want to create dependent dropdowns. For example, if someone selects book from the first dropdown, second dropdown should have chapter listed under that book. I have achieved it using HTML / Jquery / AJAX. But i am now interested to achieve same using Django forms. If anyone have idea, please share it.

Thank you in advance.

2
  • 1
    You can't do it with Javascript. Commented Jan 8, 2018 at 9:59
  • I did it with Jquery / Ajax post calls, it might not be standard way to do it. But its working. The only issue there is, its not getting retained after submit. Commented Jan 8, 2018 at 14:59

1 Answer 1

3

If you are not afraid of adding dependencies: django-select2 has an implementation of chained selects, which can be configured using the django form API. Example from their docs:

class AddressForm(forms.Form):
    country = forms.ModelChoiceField(
        queryset=Country.objects.all(),
        label=u"Country",
        widget=ModelSelect2Widget(
            model=Country,
            search_fields=['name__icontains'],
        )
    )

    city = forms.ModelChoiceField(
        queryset=City.objects.all(),
        label=u"City",
        widget=ModelSelect2Widget(
            model=City,
            search_fields=['name__icontains'],
            dependent_fields={'country': 'country'},
            max_results=500,
        )
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.