我在Django Rest框架中通过Category
过滤Brand
时面临问题
class Category(TimeStamp):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True, blank=True)
icon = models.ImageField(upload_to='category_icons', null=True, blank=True)
parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='children', null=True, blank=True)
class Brand(models.Model):
brand_name = models.CharField(max_length=150)
brand_image = models.ImageField(upload_to='brand_images', null=True, blank=True)
category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, null=True)
这里我有两个模型。例如,我有一个类似Clothing -> Foot Wear -> Shoes
的类别,如果用户输入Clothing
类别,我应该得到Clothing
中的所有品牌,但当我转到Shoes
部分时,我应该得到Shoes
类别中的所有品牌。为此,我如何编写query_set
?任何帮助都将不胜感激!提前谢谢
将相关名称添加到类别外键
然后
相关问题 更多 >
编程相关推荐