Core arguments in serializer fields
Serializer fields in Django are same as Django Form fields and Django model fields and thus require certain arguments to manipulate the behaviour of those Fields.
Argument | Description |
---|---|
read_only | Set this to True to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization |
write_only | Set this to True to ensure that the field may be used when updating or creating an instance, but is not included when serializing the representation. |
required | Setting this to False also allows the object attribute or dictionary key to be omitted from output when serializing the instance. |
default | If set, this gives the default value that will be used for the field if no input value is supplied. |
allow_null | Normally an error will be raised if None is passed to a serializer field. Set this keyword argument to True if None should be considered a valid value. |
source | The name of the attribute that will be used to populate the field. |
validators | A list of validator functions which should be applied to the incoming field input, and which either raise a validation error or simply return. |
error_messages | A dictionary of error codes to error messages. |
label | A short text string that may be used as the name of the field in HTML form fields or other descriptive elements. |
help_text | A text string that may be used as a description of the field in HTML form fields or other descriptive elements. |
initial | A value that should be used for pre-populating the value of HTML form fields. |
Serializers – Django REST Framework
The serializers in the REST framework work very similarly to Django’s Form and ModelForm classes. The two major serializers that are most popularly used are ModelSerializer and HyperLinkedModelSerialzer. This article revolves around how to use serializers from scratch in Django REST Framework to advanced serializer fields and arguments. It assumes one is familiar with How to start a project with Django REST Framework.
- Creating and Using Serializers
- ModelSerializer
- HyperLinkedModelSerializer
- Serializer Fields
- Core arguments in serializer fields