rest_framework.relations

Module Contents

method_overridden(method_name, klass, instance)

Determine if a method has been overridden.

exception ObjectValueError

Bases:ValueError

Raised when queryset.get() failed due to an underlying ValueError. Wrapping prevents calling code conflating this with unrelated errors.

exception ObjectTypeError

Bases:TypeError

Raised when queryset.get() failed due to an underlying TypeError. Wrapping prevents calling code conflating this with unrelated errors.

Bases:django.utils.six.text_type

A string like object that additionally has an associated name. We use this for hyperlinked URLs that may render as a named link in some contexts, or render as a plain URL in others.

name
classmethod __new__(self, url, obj)
__getnewargs__(self)
class PKOnlyObject(pk)

Bases:object

This is a mock object, used for when we only need the pk of the object instance, but still want to return an object with a .pk attribute, in order to keep the same interface as a regular model instance.

__str__(self)
MANY_RELATION_KWARGS = ['read_only', 'write_only', 'required', 'default', 'initial', 'source', 'label', 'help_text', 'style', 'error_messages', 'allow_empty', 'html_cutoff', 'html_cutoff_text']
class RelatedField(**kwargs)

Bases:rest_framework.fields.Field

queryset
html_cutoff
html_cutoff_text
choices
grouped_choices
classmethod __new__(cls, *args, **kwargs)
classmethod many_init(cls, *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)
run_validation(self, data=empty)
get_queryset(self)
use_pk_only_optimization(self)
get_attribute(self, instance)
get_choices(self, cutoff=None)
iter_options(self)
display_value(self, instance)
class StringRelatedField(**kwargs)

Bases:rest_framework.relations.RelatedField

A read only field that represents its targets using their plain string representation.

to_representation(self, value)
class PrimaryKeyRelatedField(**kwargs)

Bases:rest_framework.relations.RelatedField

default_error_messages
use_pk_only_optimization(self)
to_internal_value(self, data)
to_representation(self, value)
class HyperlinkedRelatedField(view_name=None, **kwargs)

Bases:rest_framework.relations.RelatedField

lookup_field = pk
view_name
default_error_messages
use_pk_only_optimization(self)
get_object(self, view_name, view_args, view_kwargs)

Return the object corresponding to a matched URL.

Takes the matched URL conf arguments, and should return an object instance, or raise an ObjectDoesNotExist exception.

get_url(self, obj, view_name, request, format)

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.

to_internal_value(self, data)
to_representation(self, value)
class HyperlinkedIdentityField(view_name=None, **kwargs)

Bases:rest_framework.relations.HyperlinkedRelatedField

A read-only field that represents the identity URL for an object, itself.

This is in contrast to HyperlinkedRelatedField which represents the URL of relationships to other objects.

use_pk_only_optimization(self)
class SlugRelatedField(slug_field=None, **kwargs)

Bases:rest_framework.relations.RelatedField

A read-write field that represents the target of the relationship by a unique ‘slug’ attribute.

default_error_messages
to_internal_value(self, data)
to_representation(self, obj)
class ManyRelatedField(child_relation=None, *args, **kwargs)

Bases:rest_framework.fields.Field

Relationships with many=True transparently get coerced into instead being a ManyRelatedField with a child relationship.

The ManyRelatedField class is responsible for handling iterating through the values and passing each one to the child relationship.

This class is treated as private API. You shouldn’t generally need to be using this class directly yourself, and should instead simply set ‘many=True’ on the relationship.

initial = []
default_empty_html = []
default_error_messages
html_cutoff
html_cutoff_text
choices
grouped_choices
get_value(self, dictionary)
to_internal_value(self, data)
get_attribute(self, instance)
to_representation(self, iterable)
get_choices(self, cutoff=None)
iter_options(self)