Serializers
auto_serializer
- headless_cms.serializers.auto_serializer(model: type[Model], override_model_serializer_fields: dict | None = None) type[ModelSerializer]
Automatically create a serializer for a given model using Django REST Framework, including
a ‘hash’ field that dynamically computes a hash representing the state of the object and its descendants.
This function simplifies the creation of serializers for models with complex relationships or custom serialization requirements. It dynamically generates serializers that handle related objects and apply specified field overrides. The ‘hash’ field is included to provide a dynamic representation of the state of the object and all its related entities.
- Parameters:
model (type[models.Model]) – The Django model class for which to create the serializer.
override_model_serializer_fields (Optional[dict], optional) – A dictionary specifying custom field serializers, allowing for customization of the generated serializers. Defaults to None.
- Returns:
A dynamically generated serializer class tailored to the specified model, capable of handling nested serialization, field overrides, and providing a dynamic hash of the object and its descendants.
- Return type:
type[serializers.ModelSerializer]
LocalizedModelSerializer
- class headless_cms.serializers.LocalizedModelSerializer(*args, **kwargs)
A base serializer for models with localized fields.
This serializer automatically maps localized fields to their corresponding serializer fields in the Django REST Framework, making it easier to work with models that have multiple language support.
- Field Mapping:
LocalizedField -> CharField
LocalizedAutoSlugField -> SlugField
LocalizedUniqueSlugField -> SlugField
LocalizedFileField -> FileField
LocalizedIntegerField -> IntegerField
LocalizedFloatField -> FloatField
LocalizedBooleanField -> BooleanField
AutoLanguageUrlField -> UrlField
LocalizedMartorField -> LocalizedMartorFieldSerializer
LocalizedBaseSerializer
- class headless_cms.serializers.LocalizedBaseSerializer(*args, **kwargs)
A base serializer for localized models.
This serializer is designed to exclude certain fields by default, making it easier to work with models that have localized fields. The excluded fields often include those that are not necessary for the serialization process or that require special handling.
- Excluded Fields:
position: Typically used for ordering and not needed in the serialized output.
content_type: Used by Django’s content types framework, often unnecessary in the output.
object_id: The ID of the related object, usually not needed in the serialized form.
This list of excluded fields can be extended by modifying the extra_exclude attribute in the Meta class.
LocalizedDynamicFileSerializer
- class headless_cms.serializers.LocalizedDynamicFileSerializer(*args, **kwargs)
A serializer for models with dynamic file fields, primarily used for LocalizedDynamicFileModel.
This serializer is designed to handle models that have dynamic file fields, such as fields for uploaded files or URLs to external resources. It adds a src field that returns the absolute URL of the file, either from a local source or an external URL.