我有3个模型Category
,Subcategory
和Product
,我在Django管理员中注册了它们,
在add product
管理页面中,我需要根据我选择的类别值来过滤子类别列表。
现在,子类别列表显示了所有子类别, 选择类别A时,只需要具有与类别A相关的子类别。
class Category(models.Model):
name = models.CharField(max_length=200,
db_index=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Subcategory(models.Model):
category = models.ForeignKey(Category,
related_name='subcategories',
on_delete=models.CASCADE)
name = models.CharField(max_length=200,
db_index=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Product(models.Model):
category = models.ForeignKey(Category,
related_name='products',
on_delete=models.CASCADE)
subcategory = models.ForeignKey(Subcategory,
related_name='products',
on_delete=models.CASCADE)