Reputation: 1089
When creating a serializer in Django REST Framework, I understand that I can create a SerializerMethodField that creates a new read-only method for a model. For example:
class AnswerSerializer(serializers.ModelSerializer):
likes_count = serializers.SerializerMethodField()
class Meta:
model = Answer
exclude = ["a_field"]
def get_likes_count(self,instance):
return instance.voters.count()
A similar thing could also be done using a property or method in the models.py file:
class Answer(models.Model):
@property
def get_likes_count(self):
return voters.count()
So my question is:
What are the pros and cons of each? I understand that SerializerMethodField is for read_only operations but aside from that are not sure.
Is it best practice to keep the models.py file for model fields only, and to use signals for update/create operations and serializers for read operations?
Upvotes: 0
Views: 709
Reputation: 32244
One advantage to using the serializer approach instead of the @property
approach is that you can use annotations
on the queryset passed to the serializer
AnswerSerializer(Answer.objects.annotate(likes_count=Count('voters')), many=True)
def get_likes_count(self, instance):
return getattr(instance, 'likes_count', None) or instance.voters.count()
This way you can use annotations to reduce the number of queries you make and have a fall back that calculates the value anyway
Upvotes: 2