django_socio_grpc.proto_serializers
Module Contents
Classes
A read-write field that represents the target of the relationship by a unique ‘slug’ attribute. And support a type converter to be sure that the field is in the correct protobuf type |
Functions
Data
API
- django_socio_grpc.proto_serializers.LIST_PROTO_SERIALIZER_KWARGS
()
- django_socio_grpc.proto_serializers.get_default_value(field_default)
- class django_socio_grpc.proto_serializers.BaseProtoSerializer(*args, **kwargs)
Bases:
rest_framework.serializers.BaseSerializer- message_to_data(message)
Protobuf message -> Dict of python primitive datatypes.
- data_to_message(data)
Protobuf message <- Dict of python primitive datatypes.
- property message
- async asave(**kwargs)
- async ais_valid(*, raise_exception=False)
- async acreate(validated_data)
- async aupdate(instance, validated_data)
- property adata
- property amessage
- classmethod many_init(*args, **kwargs)
- abstract to_proto_message()
- class _MessageToData(message, serializer)
This nested class is used to handle the conversion of a protobuf message to a dict of python primitive datatypes. It is responsible for dealing with edge cases such as partial updates and optional fields.
Initialization
- property partial_fields
- get_data()
This method allow to populate the data dictionary with None for optional field that allow_null and not send in the request. It’s also allow to deal with partial update correctly. This is mandatory for having null value received in request as DRF expect to have None value for field that are required. We can’t rely only on required True/False as in DSG if a field is required it will have the default value of it’s type (empty string for string type) and not None
When refactoring serializer to only use message we will be able to determine the default value of the field depending of the same logic followed here
set default value for field except if optional or partial update
- get_default_field_value(field: rest_framework.serializers.Field)
- get_nullable_field_value(field: rest_framework.serializers.Field, force_default: bool = False)
Check the possibility of a field to be a nullable field, considering its default value and nullability.
Args: field (Field): The field object whose value is being accessed. It should contain information about the field’s default value and nullability. force_default (bool, optional): If True, bypasses the nullability check and forces the use of the field’s default value. If still no default value raise a _NoDictData. Defaults to False.
Returns: Any: None if the field is nullable, default value if force_default or raise if none of the first two options.
Raises: _NoDictData: - If the field cannot accept a null value and
force_defaultis False. - If the field has no presence, cannot accept null values, and has no default value.
- get_partial_field_value(field: rest_framework.serializers.Field)
- get_cleaned_field_value(field: rest_framework.serializers.Field)
- class django_socio_grpc.proto_serializers.ProtoSerializer(*args, **kwargs)
Bases:
django_socio_grpc.proto_serializers.BaseProtoSerializer,rest_framework.serializers.Serializer
- class django_socio_grpc.proto_serializers.ListProtoSerializer(*args, **kwargs)
Bases:
rest_framework.serializers.ListSerializer,django_socio_grpc.proto_serializers.BaseProtoSerializer- message_to_data(message)
List of protobuf messages -> List of dicts of python primitive datatypes.
- data_to_message(data)
List of protobuf messages <- List of dicts of python primitive datatypes.
- class django_socio_grpc.proto_serializers.PropertyReadOnlyField(**kwargs)
Bases:
rest_framework.serializers.ReadOnlyField,django_socio_grpc.protobuf.proto_classes.ProtoFieldConvertible- to_proto_field(proto_field_class: django_socio_grpc.protobuf.proto_classes.ProtoField = ProtoField, **kwargs) django_socio_grpc.protobuf.proto_classes.ProtoField
- class django_socio_grpc.proto_serializers.ModelProtoSerializer(*args, **kwargs)
Bases:
django_socio_grpc.proto_serializers.ProtoSerializer,rest_framework.serializers.ModelSerializer- build_property_field(field_name, model_class)
To generate the correct types of a model property field we have to know that the field is a property, by default the field is only a ReadOnlyField. PropertyReadOnlyField has the property information we need.
- class _MessageToData(message, serializer)
Bases:
django_socio_grpc.proto_serializers.ProtoSerializerModelProtoSerializer._MessageToData should handle update/create differentially and handle the default values of model fields.
Initialization
- property model
- property updating
- get_default_field_value(field: rest_framework.serializers.Field)
- get_nullable_field_value(field: rest_framework.serializers.Field, force_default: bool = False)
Check the possibility of a field to be a nullable field, considering its default value, nullability, and the current state of the instance (e.g., updating). This override the ProtoSerializer._MessageToData.get_nullable_field_value that doesn’t know about model for default value
Args: field (Field): The field object whose value is being accessed. It should contain information about the field’s default value and nullability. force_default (bool, optional): If True, bypasses the nullability check and forces the use of the field’s default value. If still no default value raise a _NoDictData. Defaults to False.
Returns: Any: The resolved default value of the field if it is nullable or if
force_defaultis set to True.Raises: _NoDictData: - If the field cannot accept a null value and
force_defaultis False. - If the field has no presence, cannot accept null values, and has no default value.Notes: - If the instance is in an updating state (
self.updatingis True) or the field’s default isNoneorempty, the method returns None. - The method checks if the field is nullable usingself.is_field_accept_null_value.
- class django_socio_grpc.proto_serializers.BinaryField(**kwargs)
Bases:
rest_framework.serializers.FieldInitialization
- default_error_messages
None
- to_internal_value(data)
- to_representation(value)
- class django_socio_grpc.proto_serializers.SlugRelatedConvertedField(convert_type=None, **kwargs)
Bases:
rest_framework.relations.SlugRelatedFieldA read-write field that represents the target of the relationship by a unique ‘slug’ attribute. And support a type converter to be sure that the field is in the correct protobuf type
Initialization
- to_representation(obj)