Django内容类型如何工作?

2024-04-16 23:02:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这些关系:

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')

在文章/条目中添加注释很好,例如:

^{pr2}$

我可以在数据库中看到它们,但我想在模板中列出它们的具体文章/条目的详细信息。我尝试了很多我在互联网上找到的解决方案,但每次都会遇到相同的错误Unable to get repr for <class 'django.db.models.query.QuerySet'> 我的普通关系有什么问题?如何列出与特定文章/条目相关的所有评论?在

'回溯:

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.'

Tags: djangoinpyselfadddbgetobject