I have a model similar to the following (simplified):
我有一个类似于以下(简化)的模型:
models.py
class Sample(models.Model):
name=models.CharField(max_length=200)
class Action(models.Model):
samples=models.ManyToManyField(Sample)
title=models.CharField(max_length=200)
description=models.TextField()
Now, if Action.samples
would have been a ForeignKey
instead of a ManyToManyField
, when I display Action
as a TabularInline
in Sample
in the Django Admin, I would get a number of rows, each containing a nice form to edit or add another Action
. However; when I display the above as an inline using the following:
现在,如果Action.samples是一个ForeignKey而不是ManyToManyField,当我在Django Admin中将Sample显示为Sample中的TabularInline时,我会得到许多行,每行包含一个很好的表单来编辑或添加另一个Action。然而;当我使用以下内容显示上面的内联:
class ActionInline(admin.TabularInline):
model=Action.samples.through
I get a select box listing all available actions, and not a nifty form to create a new Action
.
我得到一个列出所有可用操作的选择框,而不是一个创建新Action的漂亮表单。
My question is really: How do I display the ManyToMany relation as an inline with a form to input information as described?
我的问题是:如何将ManyToMany关系显示为内联表单以输入所描述的信息?
In principle it should be possible since, from the Sample
's point of view, the situation is identical in both cases; Each Sample
has a list of Action
s regardless if the relation is a ForeignKey
or a ManyToManyRelation
. Also; Through the Sample
admin page, I never want to choose from existing Action
s, only create new or edit old ones.
原则上应该是可能的,因为从样本的角度来看,两种情况都是相同的;无论关系是ForeignKey还是ManyToManyRelation,每个Sample都有一个Actions列表。也;通过Sample admin页面,我永远不想从现有的Actions中选择,只创建new或编辑旧的Actions。
1 个解决方案
#1
0
I see your point but think of a case where you might need to use custom through model (table). In that case the admin inline form would include the fields for that intermediate model since thats the model you asked the admin to create the form for.
我明白你的观点,但想想你可能需要使用自定义模型(表)。在这种情况下,管理员内联表单将包含该中间模型的字段,因为那是您要求管理员为其创建表单的模型。
e.g.
例如
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
The admin should display the form for the Memebership model cause thats the model the editable instance is related to. In your case the through model contains only the 2 foreign keys (1 for the Action model and 1 for the Sample) ands thats why only the list of actions appear.
管理员应显示Memebership模型的表单,因为该模型与可编辑实例相关。在您的情况下,直通模型只包含2个外键(1表示Action模型,1表示Sample),这就是为什么只显示操作列表的原因。
You could do what you are asking for if django admin supported nested inlines (there is an open ticket about that).
如果django admin支持嵌套内联,那么你可以做你想要的(有一个关于它的开放票)。
#1
0
I see your point but think of a case where you might need to use custom through model (table). In that case the admin inline form would include the fields for that intermediate model since thats the model you asked the admin to create the form for.
我明白你的观点,但想想你可能需要使用自定义模型(表)。在这种情况下,管理员内联表单将包含该中间模型的字段,因为那是您要求管理员为其创建表单的模型。
e.g.
例如
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
The admin should display the form for the Memebership model cause thats the model the editable instance is related to. In your case the through model contains only the 2 foreign keys (1 for the Action model and 1 for the Sample) ands thats why only the list of actions appear.
管理员应显示Memebership模型的表单,因为该模型与可编辑实例相关。在您的情况下,直通模型只包含2个外键(1表示Action模型,1表示Sample),这就是为什么只显示操作列表的原因。
You could do what you are asking for if django admin supported nested inlines (there is an open ticket about that).
如果django admin支持嵌套内联,那么你可以做你想要的(有一个关于它的开放票)。