Skip to main content

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 TypeDescription
BooleanFieldA field for representing boolean (True/False) values.
CharFieldA field for representing character strings.
ChoiceFieldA field for representing choices from a predefined list.
DateFieldA field for representing date values.
DateTimeFieldA field for representing date and time values.
DecimalFieldA field for representing decimal numbers.
DictFieldA field for representing dictionary data.
DurationFieldA field for representing time durations.
EmailFieldA field for representing email addresses.
FieldA generic field for representing various data types.
FileFieldA field for representing file uploads.
FilePathFieldA field for representing file paths.
FloatFieldA field for representing floating-point numbers.
HiddenFieldA field for representing hidden input values.
HStoreFieldA field for representing key-value pairs stored as a string.
IPAddressFieldA field for representing IP addresses.
ImageFieldA field for representing image file uploads.
IntegerFieldA field for representing integer values.
JSONFieldA field for representing JSON-formatted data.
ListFieldA field for representing lists of items.
ModelFieldA field for representing relationships to other Django models.
MultipleChoiceFieldA field for representing multiple choices from a predefined list.
ReadOnlyFieldA read-only field for representing data that should not be modified.
RegexFieldA field for representing values matching a regular expression pattern.
SerializerMethodFieldA field for representing data returned by a serializer method.
SlugFieldA field for representing slugs (URL-friendly versions of strings).
TimeFieldA field for representing time values.
URLFieldA field for representing URLs.
UUIDFieldA field for representing Universally Unique Identifiers (UUIDs).