Dependent drop down in Django Form

6.9k Views Asked by At

I am new to django, and I am having trouble with dependent drop down lists in django forms. I have 2 model classes, MainCategory and SubCategory, where SubCategory has maincategory as the foreign key. I am trying to create a form such that, when a category is selected, the subcategories for it should populate in the drop down. I have tried How to get Interdependent dropdowns in django using Modelform and jquery? this solution, and it works after making some changes. But, I was wondering if any other django inbuilt solution exists?

Models.py

class MainCategory(models.Model):
    name = models.CharField("Category", max_length = 50)

class SubCategory(models.Model):
    name = models.CharField("Sub category", max_length = 50)
    mainCategory = models.ForeignKey(MainCategory)
1

There are 1 best solutions below

0
On

For this issue i use django-smart-select good and easy for this job, include examples.