What is the purpose of the class meta in Django?

Viewed 5537

For what purpose is the class Meta: used in the class inside the Django serializers.py file?

2 Answers

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

The Meta class can be used to define various things about the model such as the permissions, database name, singular and plural names

Related