Answer the question
In order to leave comments, you need to log in
How to display quantity in format function?
I have a CategoryProducts model which has fields title , products ... The products field has a relationship in ManyToMany and I need to display in the def __str__ count function.
class CategoryProducts(models.Model):
class Meta:
verbose_name_plural = "Продукция"
title = models.CharField(max_length=255, verbose_name='Название', db_index=True)
products = models.ManyToManyField(to='Products', related_name="product", db_index=True, blank=True, verbose_name='Продукты')
def __str__(self):
return '{}, ({})'.format(
self.title, self.products
)
Answer the question
In order to leave comments, you need to log in
def __str__(self):
return '{}, ({})'.format(self.title, self.products.count())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question