Reputation: 448
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
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 Category
s:
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 yourManyToManyField
categories
instead ofcategory
.
Upvotes: 2