unique_for_month, unique_for_year

This commit is contained in:
Tom Christie 2014-10-22 16:29:09 +01:00
parent ae53fdff9c
commit f9c0e6ee1b

View File

@ -24,7 +24,8 @@ from rest_framework.utils.field_mapping import (
ClassLookupDict ClassLookupDict
) )
from rest_framework.validators import ( from rest_framework.validators import (
UniqueForDateValidator, UniqueTogetherValidator UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator,
UniqueTogetherValidator
) )
import copy import copy
import inspect import inspect
@ -601,6 +602,22 @@ class ModelSerializer(Serializer):
) )
validators.append(validator) validators.append(validator)
if field.unique_for_month and field_name in field_names:
validator = UniqueForMonthValidator(
queryset=model_class._default_manager,
field=field_name,
date_field=field.unique_for_month
)
validators.append(validator)
if field.unique_for_year and field_name in field_names:
validator = UniqueForYearValidator(
queryset=model_class._default_manager,
field=field_name,
date_field=field.unique_for_year
)
validators.append(validator)
return validators return validators
def _get_base_fields(self): def _get_base_fields(self):