django admin 中的通用多对多关系
我在 Django 中有几个类似的模型:
class Material(models.Model):
title = models.CharField(max_length=255)
class Meta:
abstract = True
class News(Material):
state = models.PositiveSmallIntegerField(choices=NEWS_STATE_CHOICES)
class Article(Material):
genre = models.ForeignKey(Genre, verbose_name='genre')
以及模型 Topic,它与新闻和文章相关,为 ManyToMany。
我想使用通用多对多关系,就像这个案例一样。但问题是如何在 django admin 中使用默认的 ManyToMany 小部件。或者另一个方便的类似物。
UPD:如果我不使用泛型,我会编写
class News(Material):
topic = models.ManyToMany(Topic)
class Article(Material):
topic = models.ManyToMany(Topic)
And I'd get 2个相同的表来表达这些关系。我想知道我是否可以使用泛型来拥有一个中间表,因为在我的数据库中不仅新闻和文章可能有主题。新闻和文章也可能与两个或多个主题相关。
I have few similar models in Django:
class Material(models.Model):
title = models.CharField(max_length=255)
class Meta:
abstract = True
class News(Material):
state = models.PositiveSmallIntegerField(choices=NEWS_STATE_CHOICES)
class Article(Material):
genre = models.ForeignKey(Genre, verbose_name='genre')
And model Topic, which is related to News and Article as ManyToMany.
I'd like to use Generic many-to-many relationships like in this case. But question is how to use default ManyToMany widget in django admin. Or another convenient analogue.
UPD: If I didn't use generics I'd write
class News(Material):
topic = models.ManyToMany(Topic)
class Article(Material):
topic = models.ManyToMany(Topic)
And I'd get 2 identical tables that express these relationships. I wonder if I could use generics in order to have one intermediate table, because not only news and articles may have topic in my database. News and articles may be connected with 2 or more topics as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:查看http://charlesleifer.com/blog/connecting-anything-to- 不幸的是, anything-with-django/
GenericForeignKey 的支持不如foreignkey。有一个开放(并已接受)的票证,其中包含为他们提供小部件的补丁: http://code.djangoproject.com/ticket/9976 。
开箱即用的是使用 GenericForeignKey 内联管理对象
假设您的通用关系是通过
和
实现的如果您想编辑主题的新闻,您可以为新闻定义一个内联管理:
并将其添加到主题管理的内联中:
也就是说,从给定的信息中我看不到您的 ManyToMany 关系出了什么问题。似乎表达了你所需要的。
也许您正在主题中而不是新闻和文章中定义 ManyToMany 字段?在新闻和文章中定义它们。
编辑:感谢您的澄清。您的模型设置将按照 arie 的帖子进行(即相反),并且您将进行内联编辑。如果您只想从新闻/文章/等中选择现有主题。例如,我不知道 GenericRelation 有什么开箱即用的东西(它通常只是充当反向查找助手)。您可以
a) 覆盖管理表单并根据 GenericRelation 添加带有查询集的 ModelMultipleChoiceField
b) 覆盖 save() 以调整关系。
工作量相当大。我个人会坚持使用多个 m2m 表,而不是将所有内容都塞到一张表中。如果您担心在请求一个或多个主题的所有新闻和文章等时数据库会进行多次查找,那么请注意,通用解决方案将始终具有与 GenericForeignKey 的要求类似的设置,即附加列型号和 ID。这可能会导致更多的查询(例如针对每个结果的 content_type)。
EDIT: Check this out http://charlesleifer.com/blog/connecting-anything-to-anything-with-django/
GenericForeignKey's are unfortunately not as well supported as ForeignKey's. There's an open (and accepted) ticket with patch for providing a widget for them: http://code.djangoproject.com/ticket/9976
What is provided out-of-the-box is managing objects with GenericForeignKey inline.
Assuming your generic relationship is achieved by
and
If you want to edit the News of a Topic, you can define an inline admin for News:
and add it to the inlines of Topic admin:
That said, from the information given I can't see what's wrong with your ManyToMany relationship. It seems to express what you need.
Maybe you're defining the ManyToMany field in Topic instead of in News and Article? Define them in News and Article.
EDIT: Thanks for the clarification. Your model setup would be as per arie's post (i.e., the other way around) and you'd be editing inline. If you just want to select existing Topic's from inside a News/Article/etc. instance, I'm not aware of anything out-of-the-box for GenericRelation (which usually just serves as a reverse-lookup helper). You could
a) Override the admin form and add a ModelMultipleChoiceField with the queryset as per the GenericRelation
b) Override save() to adjust the relations.
Quite a lot of work. I would personally stick with multiple m2m tables and not cram everything into one. If you are afraid of the database doing multiple lookups when you ask for all News and Articles and etc. of one or more Topic's, then be aware that a generic solution will always have a similar setup to the requirements GenericForeignKey has, i.e. additional columns for model and id. That could lead to a lot more queries (e.g. against content_type for each result).
如果您只是翻转 Danny 的示例并在
Topic
-Model 一侧定义通用关系,难道不应该起作用吗?请参阅 django 文档中的示例: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#using-generic-relations-as-an-inline
适应这个问题:
另外定义每个相关模型上的反向关系。
现在您可以创建一个
TopicInline
并将主题附加到新闻、文章等......Shouldn't it work if you just turn Danny's example around and define the generic relation on the side of of the
Topic
-Model?See the example in django's docs: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#using-generic-relations-as-an-inline
Adapted to this question:
It probably makes sense to additionally define the reverse relationsship on each related model.
And now you could create a
TopicInline
and attach Topics to news, articles, whatever ...