Serialization of Response Data
ModelSerializer
moduels/{module_name}/serializers.py
from .models import User
from django_petra.petra_core import ModelSerializer, serializers
class UserSerializer(ModelSerializer):
# assign field and type casting
id = serializers.CharField()
class Meta:
model = User
fields = '__all__'
specific fields
You can specify specific fields in the Meta class
to include only the desired fields in the serialized representation:
class Meta:
fields = ['field_name', 'field_name']
modify field data
To modify the representation of a field, you can override the to_representation method in your serializer:
class UserSerializer(ModelSerializer):
# assign field and type casting
id = serializers.CharField()
class Meta:
model = User
fields = '__all__'
def to_representation(self, instance):
# Modify the representation of the object
representation = super().to_representation(instance)
representation['name'] = f"Modified: {instance.name}"
return representation
exclude fields
You can exclude specific fields from the serialized representation using the exclude_fields helper function from django_petra.raw_query.helpers
:
from django_petra.raw_query.helpers import exclude_fields
def to_representation(self, instance):
# Modify the representation of the object
representation = super().to_representation(instance)
representation['name'] = f"Modified: {instance.name}"
representation = exclude_fields(representation, [ 'password'])
return representation
fields
from django_petra.petra_core import serializers
# serializers.{fieldType}
name = serializers.CharField()
Field Type | Description |
---|---|
BooleanField | A field for representing boolean (True/False) values. |
CharField | A field for representing character strings. |
ChoiceField | A field for representing choices from a predefined list. |
DateField | A field for representing date values. |
DateTimeField | A field for representing date and time values. |
DecimalField | A field for representing decimal numbers. |
DictField | A field for representing dictionary data. |
DurationField | A field for representing time durations. |
EmailField | A field for representing email addresses. |
Field | A generic field for representing various data types. |
FileField | A field for representing file uploads. |
FilePathField | A field for representing file paths. |
FloatField | A field for representing floating-point numbers. |
HiddenField | A field for representing hidden input values. |
HStoreField | A field for representing key-value pairs stored as a string. |
IPAddressField | A field for representing IP addresses. |
ImageField | A field for representing image file uploads. |
IntegerField | A field for representing integer values. |
JSONField | A field for representing JSON-formatted data. |
ListField | A field for representing lists of items. |
ModelField | A field for representing relationships to other Django models. |
MultipleChoiceField | A field for representing multiple choices from a predefined list. |
ReadOnlyField | A read-only field for representing data that should not be modified. |
RegexField | A field for representing values matching a regular expression pattern. |
SerializerMethodField | A field for representing data returned by a serializer method. |
SlugField | A field for representing slugs (URL-friendly versions of strings). |
TimeField | A field for representing time values. |
URLField | A field for representing URLs. |
UUIDField | A field for representing Universally Unique Identifiers (UUIDs). |