Adding a ManyToManyWidget to the reverse of a ManyToManyField in the Django Admin

前端 未结 5 1044
我寻月下人不归
我寻月下人不归 2020-12-13 07:08

Let\'s say I have a simple blog app in Django 1.4:

class Post(models.Model):
    title = …
    published_on = …
    tags = models.ManyToManyField(\'Tag\')

c         


        
5条回答
  •  暖寄归人
    2020-12-13 07:29

    A bit late to the party, but this is the solution that works for me (no magic):

    # admin.py
    
    from django.contrib import admin
    from models import Post
    
    class TagPostInline(admin.TabularInline):
        model = Post.tags.through
        extra = 1
    
    class PostAdmin(admin.ModelAdmin):
        inlines = [TagPostInline]
    
    admin.site.register(Post, PostAdmin)
    

    Reference: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-models

提交回复
热议问题