I have this relationship
Class Item(models.Model):
pass
Class Category(models.Model):
items = models.ManyToManyField(Item)
I can define the field name as items for category and access it via category.items but I want to define a field name for Item too as item.categories rather than the default item.category
How can I achieve it?
Update
Tried
items = models.ManyToManyField(Item, related_name = "categories")
But I get
TypeError: Direct assignment to the reverse side of a many-to-many set is prohibited. Use categories.set() instead.
on Item.object.create(**data)
from How can I define the ManyToManyField name in django?
No comments:
Post a Comment