jwshadow
jwshadow

Reputation: 107

Django admin choice fields dynamically generation

There are Category and Subcategory fields in my django admin app. There are all subcategories choices in Subcategory field either I choose Category1 or category2. How can I create dynamically populated fields in admin app like this:

If I choose category1 I have choices in subcategories - Subcategory1, Subcategory2. If I choose Category2 - I have dynamically changed subcategory choices field with values: Subcategory3, Subcategory4.

models.py

class Category(models.Model):

    name = models.CharField(max_length=20)
    slug = models.SlugField(max_length=20)

class Subcategory(models.Model):

    name = models.CharField(max_length=15)
    slug = models.SlugField(max_length=15)
    category = models.ForeignKey(Category)

Can anyone help me with this problem? What I must to use to resolve it?

Upvotes: 2

Views: 1046

Answers (1)

onyeka
onyeka

Reputation: 1537

I'd recommend django-smart-selects, which allows you to chain related select boxes. Works in both the admin and the template.

Upvotes: 1

Related Questions