0

我创建了一个模型Comments。我想在同一张桌子上回复Comment

class Comment(models.Model):
  user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE)
  content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
  text = models.CharField(max_length=300, blank=False, null=False)
  object_id = models.PositiveIntegerField()  
  timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

  content_object = GenericForeignKey('content_type', 'object_id')

  # Relation
  reply = GenericRelation(Comment, related_query_name='reply')
  like = GenericRelation(Like, related_query_name='like')

我在这里收到此错误!

  reply = GenericRelation(Comment, related_query_name='reply')
  NameError: name 'Comment' is not defined

我该如何设置这种关系?

4

1 回答 1

0

您收到此错误,因为Comment尚未定义。

代替

reply = GenericRelation(Comment, related_query_name='reply')

reply = GenericRelation('self', related_query_name='reply')
于 2020-06-26T12:00:13.820 回答