Friday, 6 May 2022

Django Serializer && meta

 https://www.django-rest-framework.org/api-guide/serializers/

used in views.py to validate HTTP request data && send response

Meta class is used to create validators, and it also creates serilzer for you , with meta it comes with create() update() for validated fields, without meta, you will have to create serailzer on your own.



ModelSerializer 

The ModelSerializer class provides a shortcut that lets you automatically create a Serializer class with fields that correspond to the Model fields.

The ModelSerializer class is the same as a regular Serializer class, except that:

  • It will automatically generate a set of fields for you, based on the model.
  • It will automatically generate validators for the serializer, such as unique_together validators.
  • It includes simple default implementations of .create() and .update().

Declaring a ModelSerializer looks like this:

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = ['id', 'account_name', 'users', 'created']

No comments:

Post a Comment