class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields=('Comment','Comment_text','Comment_time','Comment_Post','Comment_User', )
class PostSerializers(serializers.ModelSerializer):
comment = CommentSerializer(many=True)
class Meta:
model = Postovo
fields = ('Postovo_id','Postovo_trending','comment', )
模特都是这样的
^{pr2}$下一个
class Comment(models.Model):
Comment = models.AutoField(primary_key=True)
Comment_text = models.CharField(max_length=100)
Comment_time = models.CharField(max_length=100,default=currentTimestamp)
Comment_Post = models.ForeignKey(Postovo, related_name='commentpost' ,default='1', editable=True)
Comment_User = models.ForeignKey(RegUser, related_name='commentuser' ,default='1', editable=True)
def __str__(self):
return '%s' % (self.Comment)
在视图中
class Postcomment(viewsets.ModelViewSet):
queryset = Postovo.objects.all()
serializer_class = PostSerializers
错误
AttributeError: Got AttributeError when attempting to get a value for field
comment
on serializerPostSerializers
. The serializer field might be named incorrectly and not match any attribute or key on thePostovo
instance. Original exception text was: 'Postovo' object has no attribute 'comment'.
您需要使用相关名称
commentpost
,而不是PostSerializers
中的comment
。在出现错误是因为
Postovo
实例上没有comment
属性。获取所有相关的Comment
实例的管理器可以使用related_name
commentpost
进行访问。在相关问题 更多 >
编程相关推荐