rest_witchcraft.fields module

Some SQLAlchemy specific field types.

class rest_witchcraft.fields.CharMappingField(**kwargs)[source]

Bases: rest_framework.fields.DictField

Used for Postgresql HSTORE columns for storing key-value pairs.

child = CharField(allow_null=True)
class rest_witchcraft.fields.HyperlinkedIdentityField(view_name=None, **kwargs)[source]

Bases: rest_framework.relations.HyperlinkedIdentityField

get_url(obj, view_name, request, format)[source]

Given an object, return the URL that hyperlinks to the object.

May raise a NoReverseMatch if the view_name and lookup_field attributes are not configured to correctly match the URL conf.

class rest_witchcraft.fields.ImplicitExpandableListField(**kwargs)[source]

Bases: rest_framework.fields.ListField

List field which implicitly expands parent field when child field is expanded assuming parent field is also expandable by being one of the choices.

to_internal_value(data)[source]

List of dicts of native values <- List of dicts of primitive datatypes.

class rest_witchcraft.fields.SkippableField(*, read_only=False, write_only=False, required=None, default=<class 'rest_framework.fields.empty'>, initial=<class 'rest_framework.fields.empty'>, source=None, label=None, help_text=None, style=None, error_messages=None, validators=None, allow_null=False)[source]

Bases: rest_framework.fields.Field

Field which is always skipped on to_representation.

Useful when used together with ExpandableModelSerializer since it allows to completely skip expandable field when it is not being expanded. Especially useful for OneToMany relations since by default nested serializer cannot be rendered as none of the PKs of the “many” items are known unlike ManyToOne when nested serializer can be rendered with PK. For example:

class FooSerializer(ExpandableModelSerializer):
    bar = BarSerializer(many=True)

    class Meta:
        model = Foo
        session = session
        fields = "__all__"
        expandable_fields = {
            "bar": SkippableField()
        }
get_attribute(instance)[source]

Given the outgoing object instance, return the primitive value that should be used for this field.

class rest_witchcraft.fields.UriField(view_name=None, **kwargs)[source]

Bases: rest_witchcraft.fields.HyperlinkedIdentityField

Represents a uri to the resource.

get_url(obj, view_name, request, format)[source]

Same as basic HyperlinkedIdentityField except return uri vs full url.