简体   繁体   English

如何在外键 Django 中获取计数项目?

[英]How to get count items in foreign key Django?

I have two classes of models and I need to get the number of products from a single vendor我有两类模型,我需要从单个供应商处获取产品数量

class Vendor(models.Model):
    name = models.CharField(max_length=50, unique=True)
    seo_name = models.SlugField()
    product_count = models.IntegerField(default=0)    


class Product(models.Model):
        vendor = models.ForeignKey(Vendor, unique=False, blank=True, default=None, null=True, on_delete=models.SET_NULL)

How to calculate the count of products in the Vendor ?如何计算供应商中的产品数量?

Take a look on Django recipe: Add an auto-count field to your model tutorial.看一看Django 秘诀:将自动计数字段添加到您的模型教程中。 It totally covers your question.它完全涵盖了你的问题。

Good Luck !祝你好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM