Django Admin内联,用于递归ManyToMany [英] Django Admin inline for recursive ManyToMany
本文介绍了Django Admin内联,用于递归ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下与自身具有多对多关系的模型
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
但是我一直收到此错误
<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 ?
推荐答案
可能为时已晚,但我尝试回答这个问题。 Ticket.replies.through是用于管理多对多关系的表,它具有from_ticket和to_ticket字段(使用FK来建模Ticket),您可以将此字段设置为TabularInline的选项fk_name。
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'
这篇关于Django Admin内联,用于递归ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文