drf_serpy.Fields

Field Objects

class Field(object)

Arguments:

  • attr (str): The attribute to get on the object, using the same format as operator.attrgetter. If this is not supplied, the name this field was assigned to on the serializer will be used.
  • call (bool): Whether the value should be called after it is retrieved from the object. Useful if an object has a method to be serialized.
  • label (str): A label to use as the name of the serialized field instead of using the attribute name of the field.
  • required (bool): Whether the field is required. If set to False, :meth:Field.to_value will not be called if the value is None.
  • schema_type (openapi.Schema): drf-yasg schema type of the Field, if None, schema type of the attribute of the Field will be used,

getter_takes_serializer

Set to True if the value function returned from :meth:Field.as_getter requires the serializer to be passed in as the first argument. Otherwise, the object will be the only parameter.

to_value

def to_value(value: Type[Any]) -> Union[dict, list, bool, str, int, float]

Transform the serialized value.

Override this method to clean and validate values serialized by this field. For example to implement an int field: ::

def to_value(self, value):
    return int(value)

Arguments:

  • value: The value fetched from the object being serialized.

as_getter

def as_getter(serializer_field_name: str, serializer_cls: Type["Serializer"])

Returns a function that fetches an attribute from an object.

Return None to use the default getter for the serializer defined in

Arguments:

  • serializer_field_name (str): The name this field was assigned to on the serializer.
  • serializer_cls: The Serializer this field is a part of.

get_schema

def get_schema() -> Union[None, openapi.Schema]

get the openapi.Schema of the field

Returns:

Union[None, openapi.Schema]: return the openapi.Schema for the given schema_type

StrField Objects

class StrField(Field)

A Field that converts the value to a string.

IntField Objects

class IntField(Field)

A Field that converts the value to an integer.

FloatField Objects

class FloatField(Field)

A Field that converts the value to a float.

BoolField Objects

class BoolField(Field)

A Field that converts the value to a boolean.

MethodField Objects

class MethodField(Field)

A Field that calls a method on the Serializer.

This is useful if a Field needs to serialize a value that may come from multiple attributes on an object. For example: ::

class FooSerializer(Serializer):
    plus = MethodField()
    minus = MethodField('do_minus')

    def get_plus(self, foo_obj) -> int:
        return foo_obj.bar + foo_obj.baz

    def do_minus(self, foo_obj) -> int:
        return foo_obj.bar - foo_obj.baz

foo = Foo(bar=5, baz=10)
FooSerializer(foo).data
# {'plus': 15, 'minus': -5}

Arguments:

  • method (str): The method on the serializer to call. Defaults to 'get_<field name>'.

ImageField Objects

class ImageField(Field)

A Field that converts the value to a image url.

ListField Objects

class ListField(Field)

to_value

def to_value(value: List[Union[Type[Any], bool, str, float, int]]) -> List[Union[str, int, bool, float]]

Arguments:

  • value (list): List of self.field_attrs or list of primitive types

DateField Objects

class DateField(Field)

A Field that converts the value to a date format.

DateTimeField Objects

class DateTimeField(DateField)

A Field that converts the value to a date time format.