简体   繁体   中英

Django Rest Framework: 'RelatedManager' object has no attribute 'body' error when using nested serializer

With Django DRF, I am trying to display the comments for a particular post in a blog using a nested serializer.

I have run into the following error:

'RelatedManager' object has no attribute 'body'

Here is my code:

comment model:

class Comment(models.Model):
    #adapted from https://blog.logrocket.com/use-django-rest-framework-to-build-a-blog/
    created = models.DateTimeField(auto_now_add=True)
    body = models.TextField(blank=False)
    user_id = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='comments', on_delete=models.CASCADE)
    post = models.ForeignKey('Posts', related_name='comments', on_delete=models.CASCADE)

    @property
    def time_left(self):
        return self.post.time_left

Post model:

class Posts(models.Model):
    title = models.CharField(max_length=100)
    topic = MultiSelectField(choices=TOPIC_CHOICES)
    creation_timestamp = models.DateTimeField(auto_now_add=True)
    expiration_timestamp = models.DateTimeField(default=expiration)
    body = models.CharField(max_length=255)
    user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) #related_name='posts' 
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="likes",blank=True)
    dislikes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="dislikes",blank=True)

    @property
    def is_expired(self):
        #taken from https://stackoverflow.com/questions/41505243/how-to-automatically-change-model-fields-in-django
        if now() > self.expiration_timestamp:
            return True
        return False
    
    @property
    def time_left(self):
        return self.expiration_timestamp - now()

serializers.py:

class CommentSerializer(serializers.ModelSerializer):

    time_left = serializers.ReadOnlyField()

    class Meta:
        model = Comment
        fields = ('created', 'body', 'user_id', 'post','time_left')

class PostsSerializer(serializers.ModelSerializer):
    is_expired = serializers.ReadOnlyField()
    time_left = serializers.ReadOnlyField()

    comments = CommentSerializer(source='comments.body',) ########## THIS IS THE PROBLEMATIC LINE #######

    class Meta:
        #make sure that the relevant fields are read only
        model = Posts
        fields = ('comments','title','topic','creation_timestamp','expiration_timestamp','body','user_id','likes','dislikes','is_expired','time_left') 

I believe the problematic line is the following one from serializers.py:

comments = CommentSerializer(source='comments.body',)

I think you are confusing the different keywords that a serializer can receive. Here I leave you an answer so that you understand it perfectly.

Your comments variable should look like this.

comments = CommentSerializer(many=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM