In the admin I want to use inline elements. I want category to display
the items it is related to.
But I get this error:
Exception at /admin/store/category/7/
<class 'store.models.Item'> has no ForeignKey to
<class 'store.models.Category'>
It’s true, of-course, since I chose to use Category to point to the
items it has.
But, how can I get the admin to list in-line all the items that a
given Category has?
How can I get around this error?
CONTEXT:
class Category:
items=models.ManyToManyField(Item,through='Categoryhasitem')'
class Categoryhasitem(models.Model):
category = models.ForeignKey(Category, db_column='category')
item = models.ForeignKey(Item, db_column='item')
class Item(models.Model):
id = models.AutoField(primary_key=True)
This is my admin.py file.
class ItemInline(admin.TabularInline):
model=Item
class CategoryAdmin(admin.ModelAdmin):
inlines=[ItemInline,]
class ItemAdmin(admin.ModelAdmin):
pass
admin.site.register(Category, CategoryAdmin)
admin.site.register(Item, ItemAdmin)
The syntax is slightly different to display many-to-many relations using an inline.
See the django admin docs for working with many-to-many models for more details.