rest_framework.validators

We perform uniqueness checks explicitly on the serializer class, rather the using Django’s .full_clean().

This gives us better separation of concerns, allows us to use single-step object creation, and makes it possible to switch between using the implicit ModelSerializer class and an equivalent explicit Serializer class.

Module Contents

qs_exists(queryset)
qs_filter(queryset, **kwargs)
class UniqueValidator(queryset, message=None, lookup='exact')

Bases:object

Validator that corresponds to unique=True on a model field.

Should be applied to an individual field on the serializer.

message
set_context(self, serializer_field)

This hook is called by the serializer instance, prior to the validation call being made.

filter_queryset(self, value, queryset)

Filter the queryset to all instances matching the given attribute.

exclude_current_instance(self, queryset)

If an instance is being updated, then do not include that instance itself as a uniqueness conflict.

__call__(self, value)
__repr__(self)
class UniqueTogetherValidator(queryset, fields, message=None)

Bases:object

Validator that corresponds to unique_together = (…) on a model class.

Should be applied to the serializer class, not to an individual field.

message
missing_message
set_context(self, serializer)

This hook is called by the serializer instance, prior to the validation call being made.

enforce_required_fields(self, attrs)

The UniqueTogetherValidator always forces an implied ‘required’ state on the fields it applies to.

filter_queryset(self, attrs, queryset)

Filter the queryset to all instances matching the given attributes.

exclude_current_instance(self, attrs, queryset)

If an instance is being updated, then do not include that instance itself as a uniqueness conflict.

__call__(self, attrs)
__repr__(self)
class BaseUniqueForValidator(queryset, field, date_field, message=None)

Bases:object

message
missing_message
set_context(self, serializer)

This hook is called by the serializer instance, prior to the validation call being made.

enforce_required_fields(self, attrs)

The UniqueFor<Range>Validator classes always force an implied ‘required’ state on the fields they are applied to.

filter_queryset(self, attrs, queryset)
exclude_current_instance(self, attrs, queryset)

If an instance is being updated, then do not include that instance itself as a uniqueness conflict.

__call__(self, attrs)
__repr__(self)
class UniqueForDateValidator

Bases:rest_framework.validators.BaseUniqueForValidator

message
filter_queryset(self, attrs, queryset)
class UniqueForMonthValidator

Bases:rest_framework.validators.BaseUniqueForValidator

message
filter_queryset(self, attrs, queryset)
class UniqueForYearValidator

Bases:rest_framework.validators.BaseUniqueForValidator

message
filter_queryset(self, attrs, queryset)