Lev Slinsen
Lev Slinsen

Reputation: 448

ManyToMany field returns None despite having a value

I have 2 classes: categories and pizzas. One of them is a list of categories for the object and another one is the object. Problem is, when I call an object, I can see all the fields except that ManyToMany field returns catalog.Category.None instead of the assigned value.

Here is my models.py:

class Category(models.Model):
    CLASSIC = 'Classic'
    VEGETARIAN = 'Vegetarian'
    SPICY = 'Spicy'
    TYPE = [
        (CLASSIC, 'Classic'),
        (VEGETARIAN, 'Vegetarian'),
        (SPICY, 'Spicy')
    ]

    type = models.CharField(
        max_length=100,
        choices=TYPE
    )

    def __str__(self):
        return self.type

    class Meta:
        verbose_name_plural = 'Categories'


class Pizza(models.Model):
    name = models.CharField(max_length=100)
    price = models.IntegerField()
    size = models.OneToOneField(
        Size,
        on_delete=models.CASCADE,
        primary_key=True
    )
    category = models.ManyToManyField(Category)

    def __str__(self):
        return f"name = {self.name}, size = {self.size}, category = {self.category}"

And here is my shell output:

>>> from catalog.models import Pizza
>>> from catalog.models import Category
>>> pizza = Pizza.objects.all()
>>> pizza
<QuerySet [<Pizza: name = Chicken, prices = 8, 10, category = catalog.Category.None>, <Pizza: name = Pepperoni, prices = 8, 12, category = catalog.Category.None>, <Pizza: name = Mushroom, prices = 7, 9, category = catalog.Category.None>]>
>>> cat = Category.objects.filter(pizza=1)
>>> cat
<QuerySet [<Category: Classic>]>
>>> cat = Category.objects.filter(pizza=2)
>>> cat
<QuerySet [<Category: Classic>, <Category: Spicy>]>

Upvotes: 1

Views: 736

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

A some_pizza.category will indeed return catalog.Category.None, since this is a ManyRelatedManager, and it is the expected output of a str(..) on such ManyRelatedManager. You should add .all() to render this as a list of Categorys:

class Pizza(models.Model):

    # ...

    def __str__(self):
        return f"name = {self.name}, size = {self.size}, category = {self.category.all()}"

Note: since a Pizza can have zero, one or more categories, it might be better to name your ManyToManyField categories instead of category.

Upvotes: 2

Related Questions