Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
How to use count() in serializers?

I am trying to get count of article likes,but the problem is that i am facing with various errors. Here is my code:

class ArticleLikeSerializer(serializers.ModelSerializer):

    class Meta:
        model = ArticleLike
        fields = ('id',"author","article",'timestamp')

class ArticleSerializer(serializers.ModelSerializer):
    articlelikes_set = ArticleLikeSerializer(source='articlelikes',required=False,many=True)
    total_likes = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Article
        fields = ('id','author','caption','total_likes','articlelikes_set')

    def get_total_likes(self, language):
        return articlelikes_set.count()

Here is my error:

name 'articlelikes_set' is not defined

How can i solve the problem?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

language is the object you are serializing, hence you return:

class ArticleSerializer(serializers.ModelSerializer):
    # …

    def get_total_likes(self, language):
        return language.articlelikes.count()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!