[英]Django Image model with dynamic upload_to function using generic relation
[英]Django Generic Relation woth parent model
我创建了一个 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.