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?