Django 注释反向关系
当使用 django.contrib.comments 时,是否可以将反向关系添加到有注释的模型中?
例如:
post = Post.objects.all()[0]
comments = post.comments.all()
When using django.contrib.comments is there anyway to add the reverse relationship to a model that has comments?
For example:
post = Post.objects.all()[0]
comments = post.comments.all()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您应该能够执行以下操作:
根据 反向泛型关系
Yes, you should be able to do:
per the Django docs on reverse generic relations
我想出了另一种方法来做到这一点(为什么?因为当时我不知道有任何其他方法可以做到这一点)。 它依赖于一个抽象模型类,系统中的所有模型都是从该抽象模型类派生的。 抽象模型本身有一个方法,
comments
,定义为在调用时返回与相应具体对象关联的所有注释对象的QuerySet
。 我是这样实现的:I came up with another way to do this (why? Because I wasn't aware of any other way to do that time). It relies on having an abstract model class from which all models in the system are derived. The abstract model itself has a method,
comments
, defined which when called returns aQuerySet
of all the comment objects associated with the corresponding concrete object. I implemented it thus: