For what purpose is the class Meta:
used in the class inside the Django serializers.py file?
8
votes
1 Answers
3
votes
Serializer classes can also include reusable validators that are applied to the complete set of field data. These validators are included by declaring them on an inner Meta class.
Also when you are defining a serializer then meta tags will help the serializer to bind that object in the specified format
Below are some of the examples :
While validating request data in specified format:
class EventSerializer(serializers.Serializer):
name = serializers.CharField()
room_number = serializers.IntegerField(choices=[101, 102, 103, 201])
date = serializers.DateField()
class Meta:
# Each room only has one event per day.
validators = UniqueTogetherValidator(
queryset=Event.objects.all(),
fields=['room_number', 'date']
)
While getting data from db
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['id', 'account_name', 'users', 'created']
More you can read here
Meta
class object inside a Django model class, or a form class, etc. It’s a standard pattern to attach configuration (metadata) to a set of fields that make up the model, form, or in this case, serializer. - Martijn Pieters