Creating TabularInline in Django Admin View for inherited Many2Many relation

I have a Tag Model and Mixin that is used for adding tags to entities whenever it is needed.

class Tag(models.Model):
    name = models.CharField(max_length=64, unique=True)

class TagMixin(models.Model):

    class Meta:
        abstract = True

    tags = models.ManyToManyField(Tag, blank=True)

To create new entities it works well, it implicitly creates the correspondence table for the many to many relation:

class Item(TagMixin):
    name = models.CharField(max_length=64)

But what if I want create an admin view on Item where tag is a TabularInline input ?

How should I fill the configuration:

class ItemTagInline(admin.TabularInline):
    model = ?

@admin.register(models.Item)
class ItemAdmin(admin.ModelAdmin):
    list_display = ("id", "name")
    inlines = [ItemTagInline]

You use the .through model:

class ItemTagInline(admin.TabularInline):
    model = Item.tags.through
Вернуться на верх