Django Admin inline for recursive ManyToMany

五迷三道 提交于 2020-01-01 04:22:09

问题


I have the following model with a many-to-many relationship to itself

class Ticket(models.Model):

    STATUS = (
        (0, "Open"),
        (1, "Closed"),
    )
    status = models.SmallIntegerField(default=0,choices=STATUS)
    title = models.CharField(max_length=100)
    replies = models.ManyToManyField('self')
    description = models.TextField()

i am trying to display this model as an inline in the admin, using the following code

class TicketReply(admin.TabularInline):
    model = Ticket.replies.through

however i keep getting this error

<class 'tsn.ticket.models.Ticket_replies'> has more than 1 ForeignKey to <class 'tsn.ticket.models.Ticket'>

so im i doing this wrong, or is this not supported ?


回答1:


May be its' to late, but I try answer this question. Ticket.replies.through is a table to manage many-to-many relations, it has fields from_ticket and to_ticket(FK to model Ticket) and you can set this fields as option fk_name for TabularInline.

class TicketReply(admin.TabularInline):
    model = Ticket.replies.through
    fk_name = 'from_ticket'


来源:https://stackoverflow.com/questions/8177609/django-admin-inline-for-recursive-manytomany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!