rest_framework_json_api.relations module

class rest_framework_json_api.relations.SkipDataMixin(*args, **kwargs)

Bases: object

This workaround skips “data” rendering for relationships in order to save some sql queries and improve performance

get_attribute(instance)
to_representation(*args)
class rest_framework_json_api.relations.ManyRelatedFieldWithNoData(*args, **kwargs)

Bases: SkipDataMixin, ManyRelatedField

class rest_framework_json_api.relations.HyperlinkedMixin(self_link_view_name=None, related_link_view_name=None, **kwargs)

Bases: object

get_url(name, view_name, kwargs, request)

Given a name, view name and kwargs, 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_framework_json_api.relations.HyperlinkedRelatedField(*args, **kwargs)

Bases: HyperlinkedMixin, SkipDataMixin, RelatedField

classmethod many_init(*args, **kwargs)

This method handles creating a parent ManyRelatedField instance when the many=True keyword argument is passed.

Typically you won’t need to override this method.

Note that we’re over-cautious in passing most arguments to both parent and child classes in order to try to cover the general case. If you’re overriding this method you’ll probably want something much simpler, eg:

@classmethod
def many_init(cls, *args, **kwargs):
    kwargs['child'] = cls()
    return CustomManyRelatedField(*args, **kwargs)
class rest_framework_json_api.relations.ResourceRelatedField(*args, **kwargs)

Bases: HyperlinkedMixin, PrimaryKeyRelatedField

default_error_messages = {'does_not_exist': 'Invalid pk "{pk_value}" - object does not exist.', 'incorrect_relation_type': 'Incorrect relation type. Expected {relation_type}, received {received_type}.', 'incorrect_type': 'Incorrect type. Expected resource identifier object, received {data_type}.', 'missing_id': "Invalid resource identifier object: missing 'id' attribute", 'missing_type': "Invalid resource identifier object: missing 'type' attribute", 'no_match': 'Invalid hyperlink - No URL match.', 'required': 'This field is required.'}
use_pk_only_optimization()
conflict(key, **kwargs)

A helper method that simply raises a validation error.

to_internal_value(data)
to_representation(value)
get_resource_id(value)

Get resource id of related field.

Per default pk of value is returned.

get_resource_type_from_included_serializer()

Check to see it this resource has a different resource_name when included and return that name, or None

get_parent_serializer()
is_serializer(candidate)
get_choices(cutoff=None)
class rest_framework_json_api.relations.PolymorphicResourceRelatedField(*args, **kwargs)

Bases: ResourceRelatedField

Inform DRF that the relation must be considered polymorphic. Takes a polymorphic_serializer as the first positional argument to retrieve then validate the accepted types set.

default_error_messages = {'does_not_exist': 'Invalid pk "{pk_value}" - object does not exist.', 'incorrect_relation_type': 'Incorrect relation type. Expected one of [{relation_type}], received {received_type}.', 'incorrect_type': 'Incorrect type. Expected resource identifier object, received {data_type}.', 'missing_id': "Invalid resource identifier object: missing 'id' attribute", 'missing_type': "Invalid resource identifier object: missing 'type' attribute", 'no_match': 'Invalid hyperlink - No URL match.', 'required': 'This field is required.'}
use_pk_only_optimization()
to_internal_value(data)
class rest_framework_json_api.relations.SerializerMethodFieldBase(*args, **kwargs)

Bases: Field

bind(field_name, parent)
get_attribute(instance)
class rest_framework_json_api.relations.ManySerializerMethodResourceRelatedField(*args, **kwargs)

Bases: SerializerMethodFieldBase, ResourceRelatedField

to_representation(value)
class rest_framework_json_api.relations.SerializerMethodResourceRelatedField(*args, **kwargs)

Bases: SerializerMethodFieldBase, ResourceRelatedField

Allows us to use serializer method RelatedFields with return querysets

many_kwargs = ['read_only', 'write_only', 'required', 'default', 'initial', 'source', 'label', 'help_text', 'style', 'error_messages', 'allow_empty', 'html_cutoff', 'html_cutoff_text', 'self_link_view_name', 'related_link_view_name', 'related_link_lookup_field', 'related_link_url_kwarg', 'method_name', 'model']
many_cls

alias of ManySerializerMethodResourceRelatedField

classmethod many_init(*args, **kwargs)
class rest_framework_json_api.relations.ManySerializerMethodHyperlinkedRelatedField(*args, **kwargs)

Bases: SkipDataMixin, ManySerializerMethodResourceRelatedField

class rest_framework_json_api.relations.SerializerMethodHyperlinkedRelatedField(*args, **kwargs)

Bases: SkipDataMixin, SerializerMethodResourceRelatedField

many_cls

alias of ManySerializerMethodHyperlinkedRelatedField