L
L
Little Vasya2018-06-18 10:11:19
Django
Little Vasya, 2018-06-18 10:11:19

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
        )

5b275b05c7932818988950.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-06-18
@emilov

def __str__(self):
    return '{}, ({})'.format(self.title, self.products.count())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question