altF4
altF4

Reputation: 319

Django Rest Framework comment on post api

I am developing rest apis using drf and I am trying to show comments on post detail api. I looked nested serializers and couldn't manage to implement to my project. How can I use nested serializers to show comments? and I got this error:

NameError: name 'CommentSerializer' is not defined

there is my code:

-comment model

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    author = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    content = models.TextField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)
    is_active = models.BooleanField(default=True)

    class Meta:
        ordering = ('-created_at',)

    def __str__(self):
        return f'Comment by {self.author.username} on {self.post}'

    def children(self):
        return Comment.objects.filter(parent=self)

    @property
    def is_parent(self):
        if self.parent is not None:
            return False
        return True

-post model

class Post(models.Model):
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    image = models.ImageField(upload_to=upload_location,blank=True, null=True)
    title = models.TextField(max_length=30, null=True, blank=True)
    slug = models.SlugField(unique=True, blank=True)
    created_at = models.DateTimeField(default=timezone.now)
    star_rate = models.FloatField(default=0.0)

    def __str__(self):
        return self.slug

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'slug':self.slug})

-serializers.py

class PostDetailSerializer(serializers.ModelSerializer):
    image = SerializerMethodField()
    author = SerializerMethodField()
    comments = CommentSerializer(source='comments.content')
    class Meta:
        model = Post
        fields = ('author', 'image', 'title', 'created_at','star_rate', 'slug', 'comments')

    def get_image(self, obj):
        try:
            image = obj.image.url
        except:
            image = None
        return image

    def get_author(self, obj):
        return obj.author.username

class CommentSerializer(serializers.ModelSerializer):
    reply_count = SerializerMethodField()
    author = SerializerMethodField()
    class Meta:
        model = Comment
        fields = ('content', 'parent', 'author', 'reply_count', 'post')

    def get_reply_count(self, obj):
        if obj.is_parent:
            return obj.children().count()
        return 0

    def get_author(self, obj):
        return obj.author.username

Upvotes: 0

Views: 3848

Answers (1)

Aayush Agrawal
Aayush Agrawal

Reputation: 1394

Your code is upside down. Entities being referenced need to be defined before they can be referenced. Put the CommentSerializer class before the PostDetailSerializer class.

class CommentSerializer(serializers.ModelSerializer):
    reply_count = SerializerMethodField()
    author = SerializerMethodField()
    class Meta:
        model = Comment
        fields = ('content', 'parent', 'author', 'reply_count', 'post')

    def get_reply_count(self, obj):
        if obj.is_parent:
            return obj.children().count()
        return 0

    def get_author(self, obj):
        return obj.author.username

class PostDetailSerializer(serializers.ModelSerializer):
    image = SerializerMethodField()
    author = SerializerMethodField()
    comments = CommentSerializer(source='comments.content')
    class Meta:
        model = Post
        fields = ('author', 'image', 'title', 'created_at','star_rate', 'slug', 'comments')

    def get_image(self, obj):
        try:
            image = obj.image.url
        except:
            image = None
        return image

    def get_author(self, obj):
        return obj.author.username

Upvotes: 3

Related Questions