2

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:

  • choose Category1 and I see Subcategory1, Subcategory2 in Subcategories choicefield,
  • choose Category2 and I see Subcategory3, Subcategory4 in Subcategories choicefield,

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?

jwshadow
  • 107
  • 11
  • Found similar question, this might help you. https://stackoverflow.com/questions/62939678/how-to-get-dynamic-choices-field-in-admin-panel-of-django-category-sub-category – Yespa16 Jun 04 '21 at 14:09

1 Answers1

1

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

onyeka
  • 1,517
  • 13
  • 18