本文整理汇总了Python中rest_framework.validators.UniqueValidator方法的典型用法代码示例。如果您正苦于以下问题:Python validators.UniqueValidator方法的具体用法?Python validators.UniqueValidator怎么用?Python validators.UniqueValidator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.validators
的用法示例。
在下文中一共展示了validators.UniqueValidator方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_standard_field
# 需要导入模块: from rest_framework import validators [as 别名]
# 或者: from rest_framework.validators import UniqueValidator [as 别名]
def build_standard_field(self, field_name, model_field):
field_class, field_kwargs = super(
NaturalKeySerializer, self
).build_standard_field(field_name, model_field)
if 'validators' in field_kwargs:
field_kwargs['validators'] = [
validator
for validator in field_kwargs.get('validators', [])
if not isinstance(validator, UniqueValidator)
]
return field_class, field_kwargs
示例2: __init__
# 需要导入模块: from rest_framework import validators [as 别名]
# 或者: from rest_framework.validators import UniqueValidator [as 别名]
def __init__(self, queryset, message=None, lookup=''):
"""
Setting empty string as default lookup for UniqueValidator.
For Mongoengine exact is a shortcut to query with regular experission.
This fixes https://github.com/umutbozkurt/django-rest-framework-mongoengine/issues/264
"""
super(UniqueValidator, self).__init__(queryset, message, lookup)
示例3: get_relation_kwargs
# 需要导入模块: from rest_framework import validators [as 别名]
# 或者: from rest_framework.validators import UniqueValidator [as 别名]
def get_relation_kwargs(field_name, relation_info):
"""
Creates a default instance of a flat relational field.
"""
model_field, related_model, to_many, to_field, has_through_model, reverse = relation_info
kwargs = {
'queryset': related_model._default_manager,
'view_name': get_detail_view_name(related_model)
}
if to_many:
kwargs['many'] = True
if to_field:
kwargs['to_field'] = to_field
if has_through_model:
kwargs['read_only'] = True
kwargs.pop('queryset', None)
if model_field:
if model_field.verbose_name and needs_label(model_field, field_name):
kwargs['label'] = capfirst(model_field.verbose_name)
help_text = model_field.help_text
if help_text:
kwargs['help_text'] = help_text
if not model_field.editable:
kwargs['read_only'] = True
kwargs.pop('queryset', None)
if kwargs.get('read_only', False):
# If this field is read-only, then return early.
# No further keyword arguments are valid.
return kwargs
if model_field.has_default() or model_field.blank or model_field.null:
kwargs['required'] = False
if model_field.null:
kwargs['allow_null'] = True
if model_field.validators:
kwargs['validators'] = model_field.validators
if getattr(model_field, 'unique', False):
validator = UniqueValidator(queryset=model_field.model._default_manager)
kwargs['validators'] = kwargs.get('validators', []) + [validator]
if to_many and not model_field.blank:
kwargs['allow_empty'] = False
return kwargs