drf_shortcuts.serializers
serializers module of DRF (Django REST Framework) shortcuts package.
Functions:
-
generate_detail_view_name
: Generates detail view name for a viewset which DRF uses to expose it. -
generate_serializer_base_name
: Generates base name to be used in viewset routing via DRF router further on. -
get_entity_pk
: Gets the primary key (PK) of an entity associated with the serializer provided. -
get_optional_field_value
: Gets the value of a model field if it exists either in serializer data or in the database. -
get_required_field_value
: Gets the value of a model field either from serializer data or from the database. -
rename_serializer_field
: Renames specified field of a serializer optionally updating its label. -
create_standard_serializer_class
: Creates serializer class for the Django model specified.
Classses:
-
JsFriendlyFieldsRenamingSerializer
: Renames serializer fields from snake_case into Javascript-friendly PascalCase. -
OptimizeUrlFieldsSerializer
: Removes serializer fields producing URLs from output reducing resulting output size. -
UpdateEditorSerializer
: Automatically updates serializer data with request.user if there is any in case of update. -
InsertAuthorSerializer
: Automatically adds request.user to serializer data if there is any in case of creation.
generate_detail_view_name
generate_detail_view_name(base_name)
Generates detail view name for a viewset which DRF will use to expose it via router. Detail view is used to process requests against individual entities rather than lists. DRF registers base_name + '-detail' by default as detail view name.
Takes API_URL_NAMESPACE
setting into account if set,
i.e. if API_URL_NAMESPACE
is 'foo' and base name is 'bar' then detail view full name would be 'foo:bar-detail'.
The intended usage is to provide HyperlinkedRelatedFields
with proper view name to resolve.
Parameters:
- base_name
str
: The base name of a viewset to.
Returns: the detail view name.
See also:
- Usage of routers in DRF
generate_serializer_base_name
rest_framework.relations.HyperlinkedRelatedField
generate_serializer_base_name
generate_serializer_base_name(model_cls)
Generates base name to be used when a viewset is exposed via DRF router further on. Attaching a base name to the serializer streamlines routing to detail views from related fields.
By convention standard serializers created by DRF shortcuts package have DEFAULT_BASE_NAME
class attribute,
which captures this function result against serializer model class for further usage.
Parameters:
- model_cls
django.db.models.base.ModelBase
: The Model class serializer works with.
Returns: the base name.
See also:
- Usage of routers in DRF
generate_detail_view_name
get_entity_pk
get_entity_pk(serializer)
Gets the primary key (PK) of an entity associated with the serializer provided. Looks up serializer context for associated view data.
Parameters:
- serializer
rest_framework.serializers.BaseSerializer
: The serializer instance to use for PK lookup.
Returns: the PK of the entity if present else None
.
get_optional_field_value
get_optional_field_value(data, field_name, pk, fetch_model)
Gets the value of a model field if it exists either in serializer data or in the database. "Optional" means it's not an issue if the field value is not present.
If there's no field in data, then model if fetched via function provided and looked up instead. Field can be missing in data in case of partial update (PATCH) or in case action allows that some other way.
Parameters:
- data
dict
: The serializer's data to look up the field in first. - field_name
str
: The name of the field to look up. - pk
object
: The value of corresponding entity's PK to fetch model in case there's no field in data. - fetch_model
function
: A function which is expected to return model instance if executed with its PK as argument.
Returns: the value of the field if present else None
.
get_required_field_value
get_required_field_value(data, field_name, pk, fetch_model)
Gets the value of a model field either from serializer data or from the database. "Required" means it's an issue if the field value is not present hence function will fail in such case.
If there's no field in data, then model if fetched via function provided and looked up instead. Field can be missing in data in case of partial update (PATCH) or in case action allows that some other way.
Parameters:
- data
dict
: The serializer's data to look up the field in first. - field_name
str
: The name of the field to look up. - pk
object
: The value of corresponding entity's PK to fetch model in case there's no field in data. - fetch_model
function
: A function which is expected to return model instance if executed with its PK as argument.
Returns: the value of the field if present else None
.
rename_serializer_field
rename_serializer_field(serializer, source_name, target_name, display_name=None)
Renames specified field of a serializer optionally updating its label.
Useful to make field set of a serialized representation of an entity differ from the original one.
Parameters:
- serializer
rest_framework.serializers.Serializer
: The serializer instance to rename field of. - source_name
str
: The original name of the field. - target_name
str
: The desired name of the field. - display_name
str
: The updated label of the field (optional).
See also:
JsFriendlyFieldsRenamingSerializer
JsFriendlyFieldsRenamingSerializer(self, instance=None, data=<class 'rest_framework.fields.empty'>, **kwargs)
Renames serializer fields from snake_case into Javascript-friendly PascalCase.
Looks up field names in snake_case and replaces with PascalCase names leveraging rename_serializer_field & RegExp.
See also:
rename_serializer_field
OptimizeUrlFieldsSerializer
OptimizeUrlFieldsSerializer(self, instance=None, data=<class 'rest_framework.fields.empty'>, **kwargs)
Removes serializer fields producing URLs from output reducing resulting output size.
By default URL fields are removed for any Renderer except BrowsableAPIRenderer. HyperlinkedIdentityField, HyperlinkedRelatedField and any explicitly added fields are stripped out. Behavior can be overridden by "forceUrls" query parameter ("true" / "false").
To explicitly add a field inheritors should set up explicit_url_field_names
class attribute.
See also:
explicit_url_field_names
list() -> new empty list list(iterable) -> new list initialized from iterable's items
UpdateEditorSerializer
UpdateEditorSerializer(self, instance=None, data=<class 'rest_framework.fields.empty'>, **kwargs)
Automatically updates serializer data with request.user if there is any in case of update.
The intended usage is streamline update of "last modified by" kind of model fields. Only PUT & PATCH methods trigger such behavior.
Inheritors must either define editor_field_name
or implement set_editor_core
method.
InsertAuthorSerializer
InsertAuthorSerializer(self, instance=None, data=<class 'rest_framework.fields.empty'>, **kwargs)
Automatically adds request.user to serializer data if there is any in case of creation.
The intended usage is streamline update of "created by" kind of model fields. Only POST methods trigger such behavior.
Inheritors must either define author_field_name
or implement set_author_core
method.
create_standard_serializer_class
create_standard_serializer_class(model_cls)
Creates serializer class for the Django model specified.
Created serializer will declare all model fields,
will have "url" HyperlinkedIdentityField
pointing at detail view for the entity
and will inherit OptimizeUrlFieldsSerializer
and JsFriendlyFieldsRenamingSerializer
behaviors.
Parameters:
- model_cls
django.db.models.base.ModelBase
: The Model class serializer should work with.
Returns: the standardized serializer class for the model specified.