我使用标准的django.contrb.comments app ...
class BaseCommentAbstractModel(models.Model):
"""
An abstract base class that any custom comment models probably should
subclass.
"""
# Content-object field
content_type = models.ForeignKey(ContentType,
verbose_name=_('content type'),
related_name="content_type_set_for_contrib_%(class)s")
object_pk = models.TextField(_('object ID'))
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
...
需要获得与通用关系相关的相关对象的注释列表:
from django.contrib.comments import Comment
comment_list = list(Comment.objects.values_list('id', 'content_object')
但它会引发错误:
FieldError:无法将关键字“content_object”解析为字段。选项包括:comment,content_type,flags,id,ip_address,is_public,is_removed,object_pk,site,submit_date,user,user_email,user_name,user_url
有办法做到这一点吗?
答案 0 :(得分:0)
content_object
是GenericForeignKey
字段。您应该指定object_pk
(fk字段):
Comment.objects.values_list('id', 'object_pk')
<强>更新强>
获取评论ID,对象的网址:
[(pk, urlresolvers.reverse('comments-url-redirect', args=(ct, o)))
for pk, o, ct in Comment.objects.values_list('id', 'object_pk', 'content_type')]