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. It totally covers your question.
Good Luck !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.