计算深度嵌套对象django的特定字段

时间:2020-10-15 14:47:51

标签: python django postgresql django-rest-framework

我有一个模型Category,它有孩子,而且Product模型看起来像这样

class Category(models.Model):
    name = models.CharField(max_length=200)
    category = models.ForeignKey(Category, related_name='children')
    product_count = models.IntegerField(null=True)

class Product(models.Model):
   category = models.ForeignKey(Category)
   sub_category = models.ForeignKey(Category, related_name='sub_category_products')
   name = models.CharField(max_length=300)
   price = models.DecimalField()

,我需要计算属于指定类别的所有产品,它可以属于该类别的子类别。类别模型可以深度嵌套。我该如何查询!任何将不胜感激。预先感谢!

0 个答案:

没有答案