Django Admin内联递归ManyToMany

发布于 2024-12-16 20:02:28 字数 661 浏览 2 评论 0原文

我有以下模型,其本身具有多对多关系,

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()

我试图使用以下代码将此模型显示为管理中的内联模型,

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

但是我不断收到此错误

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

,所以我做错了,或者这不是支持?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-12-23 20:02:28

可能已经太晚了,但我尝试回答这个问题。 Ticket.replies.through 是一个管理多对多关系的表,它有字段 from_ticket 和 to_ticket(FK to model Ticket),您可以将此字段设置为 TabularInline 的选项 fk_name 。

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

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文