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

Models are like this

class Postovo(models.Model):

Postovo_id = models.AutoField(primary_key=True)
Postovo_type = models.ForeignKey(Type, related_name='posttype' ,default='1', editable=True)
Postovo_time = models.CharField(max_length=100,default=currentTimestamp, editable=True)
Postovo_link1 = models.CharField(max_length=1000,default='linkofimage1', editable=True)
Postovo_link2 = models.CharField(max_length=1000,default='linkofimage2', editable=True)
Postovo_person1=models.CharField(max_length=100,default='person1', editable=True)
Postovo_person2=models.CharField(max_length=100,default='person2', editable=True)
Postovo_hot=models.CharField(max_length=100,default='False', editable=True)
Postovo_trending=models.CharField(max_length=100,default='False', editable=True)


def __str__(self):
    return '%s' % (self.Postovo_id)

Next

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)  

In views

class Postcomment(viewsets.ModelViewSet):

queryset = Postovo.objects.all()
serializer_class = PostSerializers

ERROR

AttributeError: Got AttributeError when attempting to get a value for field comment on serializer PostSerializers. The serializer field might be named incorrectly and not match any attribute or key on the Postovo instance. Original exception text was: 'Postovo' object has no attribute 'comment'.

1 Answer 1

3

You need to use the related name commentpost instead of comment in PostSerializers.

class PostSerializers(serializers.ModelSerializer):
    commentpost = CommentSerializer(many=True)

    class Meta:
    model = Postovo
    fields = ('Postovo_id','Postovo_trending','commentpost', )

The error is coming because there is no comment attribute on a Postovo instance. The manager for getting all the related Comment instances is accessible using the related_name commentpost.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked :) @rahul thank you very much actually this silly thing is not mentioned anywhere .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.