Django Generic Relation woth parent model



我创建了一个模型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

如何设置此关系?

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

取代

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

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

相关内容

  • 没有找到相关文章

最新更新