将 models.ManyToManyField 显示为内联,与 models.ForeignKey 内联形式相同

发布于 2024-08-26 09:40:40 字数 1016 浏览 6 评论 0原文

我有一个类似于以下内容的模型(简化):

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

现在,如果 Action.samplesForeignKey 而不是 ManyToManyField,当我在 Django Admin 的 Sample 中将 Action 显示为 TabularInline,我会得到许多行,每行都包含一个可供编辑的漂亮表单或添加另一个操作。然而;当我使用以下内容将以上内容显示为内联时:

class ActionInline(admin.TabularInline):
    model=Action.samples.through

我得到一个列出所有可用操作的选择框,而不是一个用于创建新 Action 的漂亮表单。

我的问题确实是:如何将多对多关系显示为内联表单以输入所描述的信息?

原则上这应该是可能的,因为从Sample的角度来看,这两种情况的情况是相同的;每个 Sample 都有一个 Action 列表,无论关系是 ForeignKey 还是 ManyToManyRelation。还;通过 Sample 管理页面,我不想从现有的 Action 中进行选择,只想创建新的或编辑旧的。

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:

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.

My question is really: How do I display the ManyToMany relation as an inline with a form to input information as described?

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 Actions regardless if the relation is a ForeignKey or a ManyToManyRelation. Also; Through the Sample admin page, I never want to choose from existing Actions, only create new or edit old ones.

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

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

发布评论

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

评论(1

错爱 2024-09-02 09:40:41

我明白你的观点,但想一想你可能需要通过模型(表)使用自定义的情况。在这种情况下,管理员内联表单将包含该中间模型的字段,因为这是您要求管理员为其创建表单的模型。

例如,

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)

管理员应该显示会员资格模型的表单,因为这是与可编辑实例相关的模型。
在您的情况下,通过模型仅包含 2 个外键(1 个用于操作模型,1 个用于示例),这就是为什么仅显示操作列表的原因。

如果 django admin 支持嵌套内联,您可以执行您所要求的操作(有一个开放的票证关于那个)。

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.

You could do what you are asking for if django admin supported nested inlines (there is an open ticket about that).

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