我有这些关系:
class Article(models.Model):
title = models.CharField(max_length=60)
body = models.TextField(max_length=300)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
pub_date = models.DateField()
comments_count = models.IntegerField(default=0)
objects = PublishedManager()
comments = GenericRelation(Comment)
class Entry(models.Model):
title = models.CharField(max_length=60)
body = models.TextField(max_length=300)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
pub_date = models.DateTimeField()
comments_count = models.IntegerField()
objects = PublishedManager()
comments = GenericRelation('Comment')
class Comment(models.Model):
body = models.CharField(max_length=300)
created_at = models.DateTimeField(auto_now_add=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
为文章/条目添加评论很好,例如:
position = Article.objects.get(pk=pk)
new_comment = Comment(content_object=position, body=form.cleaned_data['body'])
new_comment.save()
我可以在数据库中看到它们但我想在模板中列出特定文章/条目的详细信息。我尝试了很多我在互联网上找到的解决方案,但我每次都得到同样的错误Unable to get repr for <class 'django.db.models.query.QuerySet'>
我的通用关系出了什么问题?如何列出与特定文章/条目相关的所有评论?
&#39;回溯:
File "C:\Python35\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Python35\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python35\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Python35\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "C:\Python35\lib\site-packages\django\views\generic\detail.py" in get
118. context = self.get_context_data(object=self.object)
File "C:/xd/blog_application\blog\views.py" in get_context_data
56. e = Comment.objects.filter(content_type__model='entry', content_object=entry)
File "C:\Python35\lib\site-packages\django\db\models\manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python35\lib\site-packages\django\db\models\query.py" in filter
790. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python35\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
808. clone.query.add_q(Q(*args, **kwargs))
File "C:\Python35\lib\site-packages\django\db\models\sql\query.py" in add_q
1243. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Python35\lib\site-packages\django\db\models\sql\query.py" in _add_q
1269. allow_joins=allow_joins, split_subq=split_subq,
File "C:\Python35\lib\site-packages\django\db\models\sql\query.py" in build_filter
1149. lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "C:\Python35\lib\site-packages\django\db\models\sql\query.py" in solve_lookup_type
1035. _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "C:\Python35\lib\site-packages\django\db\models\sql\query.py" in names_to_path
1316. "adding a GenericRelation." % name
Exception Type: FieldError at /entry/4/
Exception Value: Field 'content_object' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.'