django admin many-to-many intermediary models using through= and filter_horizontal

后端 未结 3 1874
悲哀的现实
悲哀的现实 2020-12-05 03:08

This is how my models look:

class QuestionTagM2M(models.Model):
    tag = models.ForeignKey(\'Tag\')
    question = models.ForeignKey(\'Question\')
    date_         


        
3条回答
  •  渐次进展
    2020-12-05 03:35

    The docs may have changed since the previous answers were posted. I took a look at the django docs link that @Irfan mentioned and it seems to be a more straight forward then it used to be.

    Add an inline class to your admin.py and set the model to your M2M model

    class QuestionTagM2MInline(admin.TabularInline):
        model = QuestionTagM2M
        extra = 1
    

    set inlines in your admin class to contain the Inline you just defined

    class QuestionAdmin(admin.ModelAdmin):
        #...other stuff here
        inlines = (QuestionTagM2MInline,)
    

    Don't forget to register this admin class

    admin.site.register(Question, QuestionAdmin)
    

    After doing the above when I click on a question I have the form to do all the normal edits on it and below that are a list of the elements in my m2m relationship where I can add entries or edit existing ones.

提交回复
热议问题