Home > OS >  How to prevent Django serializer get method executing multiple times
How to prevent Django serializer get method executing multiple times

Time:12-07

I have a serializer making calls to other serializers in order to produce one Article object containing all data required for the frontend.

Everything works as expected except the get_sentiment() method is called multiple times and I can't figure out why.

I tried to copy the behaviour from the Tag serializer, which produces one list containing all of the tags required. For some reason the article_sentiment field produces multiple lists.

If there is any insight on why DRF is producing the behaviour it'll be much appreciated.

Here are the serializers.

class TagSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            'tag',
        )
        model = Tag
        
       
class SentimentSerializer(serializers.Serializer):
    sentiment=serializers.SerializerMethodField()
    class Meta: 
        fields = (
            'sentiment',
        )
        model = Reaction
        
    def get_sentiment(self, obj):
        likes = Reaction.objects.filter(article_id=obj.article_id, sentiment=1).count()
        dislikes = Reaction.objects.filter(article_id=obj.article_id, sentiment=2).count()
        if likes   dislikes == 0:
            return 0
        else:
            percent = (likes / (likes   dislikes)) * 100
            print(obj.article_id.title   str(percent))
            items = [likes, dislikes, percent]
            return items
            
    


"""
Gets the full details of an Article
"""
class ArticleSerializer(serializers.ModelSerializer):
    article_tags = TagSerializer(many=True, read_only=True)
    article_sentiment = SentimentSerializer(many=True, read_only=True)
    class Meta:
        fields = (
            'id',
            'title',
            'content',
            'article_tags',
            'pub_date',
            'preview_image',
            'author',
            'author_profile_name',
            'article_sentiment',
        )
        model = Article

Here is the ouput output of API call

thank you

CodePudding user response:

Move the SerializerMethodField into ArticleSerializer:

class TagSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            'tag',
        )
        model = Tag

"""
Gets the full details of an Article
"""
class ArticleSerializer(serializers.ModelSerializer):
    article_tags = TagSerializer(many=True, read_only=True)
    sentiment = serializers.SerializerMethodField()
    class Meta:
        fields = (
            'id',
            'title',
            'content',
            'article_tags',
            'pub_date',
            'preview_image',
            'author',
            'author_profile_name',
            'sentiment',
        )
        model = Article

    def get_sentiment(self, obj):
        likes = Reaction.objects.filter(article_id=obj.id, sentiment=1).count()
        dislikes = Reaction.objects.filter(article_id=obj.id, sentiment=2).count()
        if likes   dislikes == 0:
            return 0
        else:
            percent = (likes / (likes   dislikes)) * 100
            print(obj.title   str(percent))
            items = [likes, dislikes, percent]
            return items
  • Related