Django Admin:多对多列表框不显示通过参数
我有以下模型:
class Message(models.Model):
date = models.DateTimeField()
user = models.ForeignKey(User)
thread = models.ForeignKey('self', blank=True, null=True)
...
class Forum(models.Model):
name = models.CharField(max_length=24)
messages = models.ManyToManyField(Message, through="Message_forum", blank=True, null=True)
...
class Message_forum(models.Model):
message = models.ForeignKey(Message)
forum = models.ForeignKey(Forum)
status = models.IntegerField()
position = models.IntegerField(blank=True, null=True)
tags = models.ManyToManyField(Tag, blank=True, null=True)
在管理站点中,当我去添加/更改论坛时,我没有看到您期望的消息列表框。但是,如果我删除 ManyToManyField 声明中的“through”参数,它就会显示。这是怎么回事?我已将所有三个模型(加上标签)注册到 admin.py 中的管理站点。
TIA
I have the following models:
class Message(models.Model):
date = models.DateTimeField()
user = models.ForeignKey(User)
thread = models.ForeignKey('self', blank=True, null=True)
...
class Forum(models.Model):
name = models.CharField(max_length=24)
messages = models.ManyToManyField(Message, through="Message_forum", blank=True, null=True)
...
class Message_forum(models.Model):
message = models.ForeignKey(Message)
forum = models.ForeignKey(Forum)
status = models.IntegerField()
position = models.IntegerField(blank=True, null=True)
tags = models.ManyToManyField(Tag, blank=True, null=True)
In the admin site, when I go to add/change a forum, I don't see the messages listbox as you'd expect. However, it shows up if I remove the 'through' parameter in the ManyToManyField declaration. What's up with that? I've registered all three models (plus Tag) to the admin site in admin.py.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文档说:
但即使定义了
through
属性,也可能在管理更改视图中显示 M2M 字段。Documentation says:
But it's probably possible to display M2M fields in the admin change view even if the
through
attribute is defined.看看 官方文档:
Take a look at the official documentation:
我从 @Fedor 的回答中学到了很多东西,但一些评论和清理可能仍然有益。
有一个警告:如果模型中有另一种多对多关系(即没有通过),
super(ForumAdminForm, self).save(self, commit)
将设置 self.save_m2m casecommit
为 False。但是,调用此函数会导致错误,因为此函数也尝试使用 through 保存多对多。您可能需要手动保存所有其他多对多关系,或者捕获异常,或者其他。I learned a lot from @Fedor's answer, but some comments and cleanup may be still beneficial.
There's one caveat: if you have another many-to-many relationship in the models (that is without through),
super(ForumAdminForm, self).save(self, commit)
will set self.save_m2m in casecommit
is False. However, calling this would cause an error, because this function also tries to save the many-to-many with through as well. You may need to save all other many-to-many relationship manually, or catch the exception, or else.Django admin 很好地支持使用
through
参数的多对多中介模型。例如,您有这些
Person
和Group
模型以及中间Membership
模型:models.py
Now in
admin.py 文件,
为中间
Membership
模型定义内联类:并在模型的管理视图中使用它们:
官方文档中的更多信息:
模型,
管理员
Django admin nicely support many-to-many intermediary models that using the
through
argument .For example you have these
Person
andGroup
models with intermediateMembership
model:models.py
Now in
admin.py
file ,Define an inline class for the intermediate
Membership
model:And use them in admin views of models:
More info in official docs:
Models,
Admin