本文整理汇总了Python中django.core.validators.MinValueValidator方法的典型用法代码示例。如果您正苦于以下问题:Python validators.MinValueValidator方法的具体用法?Python validators.MinValueValidator怎么用?Python validators.MinValueValidator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.validators
的用法示例。
在下文中一共展示了validators.MinValueValidator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def __init__(self, *args, **kwargs):
try:
self.model_field = kwargs.pop('model_field')
except KeyError:
raise ValueError("ModelField requires 'model_field' kwarg")
self.min_length = kwargs.pop('min_length',
getattr(self.model_field, 'min_length', None))
self.max_length = kwargs.pop('max_length',
getattr(self.model_field, 'max_length', None))
self.min_value = kwargs.pop('min_value',
getattr(self.model_field, 'min_value', None))
self.max_value = kwargs.pop('max_value',
getattr(self.model_field, 'max_value', None))
super(ModelField, self).__init__(*args, **kwargs)
if self.min_length is not None:
self.validators.append(validators.MinLengthValidator(self.min_length))
if self.max_length is not None:
self.validators.append(validators.MaxLengthValidator(self.max_length))
if self.min_value is not None:
self.validators.append(validators.MinValueValidator(self.min_value))
if self.max_value is not None:
self.validators.append(validators.MaxValueValidator(self.max_value))
示例2: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def validators(self):
# These validators can't be added at field initialization time since
# they're based on values retrieved from `connection`.
validators_ = super().validators
internal_type = self.get_internal_type()
min_value, max_value = connection.ops.integer_field_range(internal_type)
if min_value is not None:
for validator in validators_:
if isinstance(validator, validators.MinValueValidator) and validator.limit_value >= min_value:
break
else:
validators_.append(validators.MinValueValidator(min_value))
if max_value is not None:
for validator in validators_:
if isinstance(validator, validators.MaxValueValidator) and validator.limit_value <= max_value:
break
else:
validators_.append(validators.MaxValueValidator(max_value))
return validators_
示例3: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def validators(self):
# These validators can't be added at field initialization time since
# they're based on values retrieved from `connection`.
validators_ = super(IntegerField, self).validators
internal_type = self.get_internal_type()
min_value, max_value = connection.ops.integer_field_range(internal_type)
if min_value is not None:
for validator in validators_:
if isinstance(validator, validators.MinValueValidator) and validator.limit_value >= min_value:
break
else:
validators_.append(validators.MinValueValidator(min_value))
if max_value is not None:
for validator in validators_:
if isinstance(validator, validators.MaxValueValidator) and validator.limit_value <= max_value:
break
else:
validators_.append(validators.MaxValueValidator(max_value))
return validators_
示例4: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, *args, **kwargs):
self.min_value = min_value
self.max_value = max_value
validators = kwargs.pop('validators', [])
if min_value:
validators.append(MinValueValidator(min_value))
if max_value:
validators.append(MaxValueValidator(max_value))
return super(IntegerRangeField, self).__init__(
verbose_name=verbose_name,
name=name,
validators=validators,
*args,
**kwargs
)
示例5: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def __init__(self, request, instance, *args, **kwargs):
super(SnapshotDefineSerializer, self).__init__(request, instance, *args, **kwargs)
if not kwargs.get('many', False):
dc_settings = request.dc.settings
# Limit maximum number of snapshots - Issue #chili-447
if dc_settings.VMS_VM_SNAPSHOT_LIMIT_AUTO is None:
min_count, max_count = RETENTION_MIN, RETENTION_MAX
else:
min_count, max_count = 1, int(dc_settings.VMS_VM_SNAPSHOT_LIMIT_AUTO)
self.fields['retention'].validators.append(validators.MinValueValidator(min_count))
self.fields['retention'].validators.append(validators.MaxValueValidator(max_count))
if instance.vm.is_kvm():
self._update_fields_ = list(self._update_fields_)
self._update_fields_.append('fsfreeze')
示例6: clean
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def clean(self, value):
value = super().clean(value)
if value is not None:
# Exit early if this is percentage value.
if is_percentage(value):
return value
else:
value = machine_readable_bytes(value)
# Run validation again, but with the min and max validators. This is
# because the value has now been converted to an integer.
self.validators = []
if self.min_value is not None:
self.validators.append(MinValueValidator(self.min_value))
if self.max_value is not None:
self.validators.append(MaxValueValidator(self.max_value))
self.run_validators(value)
return value
示例7: test_sets_up_fields_based_on_pod_no_max_cpu_speed
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def test_sets_up_fields_based_on_pod_no_max_cpu_speed(self):
request = MagicMock()
pod = make_pod_with_hints()
pod.hints.cpu_speed = 0
pod.save()
form = ComposeMachineForm(request=request, pod=pod)
self.assertThat(
form.fields["cpu_speed"],
MatchesStructure(
required=Equals(False),
validators=MatchesSetwise(
MatchesAll(
IsInstance(MinValueValidator),
MatchesStructure(limit_value=Equals(300)),
)
),
),
)
示例8: initialize
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def initialize(self, grade_source):
self.fields['grade'].validators.extend([
MinValueValidator(grade_source.lower_bound),
MaxValueValidator(grade_source.upper_bound),
])
self.fields['grade'].widget.attrs['size'] = 5
示例9: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def __init__(self, max_value=None, min_value=None, *args, **kwargs):
self.max_value, self.min_value = max_value, min_value
if kwargs.get('localize') and self.widget == NumberInput:
# Localized number input is not well supported on most browsers
kwargs.setdefault('widget', super(IntegerField, self).widget)
super(IntegerField, self).__init__(*args, **kwargs)
if max_value is not None:
self.validators.append(validators.MaxValueValidator(max_value))
if min_value is not None:
self.validators.append(validators.MinValueValidator(min_value))
示例10: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def validators(self):
# These validators can't be added at field initialization time since
# they're based on values retrieved from `connection`.
range_validators = []
internal_type = self.get_internal_type()
min_value, max_value = connection.ops.integer_field_range(internal_type)
if min_value is not None:
range_validators.append(validators.MinValueValidator(min_value))
if max_value is not None:
range_validators.append(validators.MaxValueValidator(max_value))
return super(IntegerField, self).validators + range_validators
示例11: test_with_validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def test_with_validators(self):
field = encrypt(
models.IntegerField(validators=[validators.MinValueValidator(1)]))
field.clean(1, None)
with self.assertRaises(exceptions.ValidationError) as cm:
field.clean(0, None)
self.assertEqual('Ensure this value is greater than or equal to 1.',
cm.exception.messages[0])
示例12: get_fields
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def get_fields(self):
fields = super().get_fields()
fields['subname'].validators.append(ReadOnlyOnUpdateValidator())
fields['type'].validators.append(ReadOnlyOnUpdateValidator())
fields['ttl'].validators.append(MinValueValidator(limit_value=self.domain.minimum_ttl))
return fields
示例13: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def __init__(self, *, max_value=None, min_value=None, **kwargs):
self.max_value, self.min_value = max_value, min_value
if kwargs.get('localize') and self.widget == NumberInput:
# Localized number input is not well supported on most browsers
kwargs.setdefault('widget', super().widget)
super().__init__(**kwargs)
if max_value is not None:
self.validators.append(validators.MaxValueValidator(max_value))
if min_value is not None:
self.validators.append(validators.MinValueValidator(min_value))
示例14: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def validators(self):
# These validators can't be added at field initialization time since
# they're based on values retrieved from `connection`.
validators_ = super().validators
internal_type = self.get_internal_type()
min_value, max_value = connection.ops.integer_field_range(internal_type)
if (min_value is not None and not
any(isinstance(validator, validators.MinValueValidator) and
validator.limit_value >= min_value for validator in validators_)):
validators_.append(validators.MinValueValidator(min_value))
if (max_value is not None and not
any(isinstance(validator, validators.MaxValueValidator) and
validator.limit_value <= max_value for validator in validators_)):
validators_.append(validators.MaxValueValidator(max_value))
return validators_
示例15: validate
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MinValueValidator [as 别名]
def validate(self, data):
errors = {}
request = self.context['request']
card = None
if request.method == 'POST':
try:
data['card'] = models.Card.objects.get(pk=request.POST['card'])
card = data['card']
except (ObjectDoesNotExist, KeyError):
if 'card' not in request.POST:
errors['card'] = 'This field is required'
else:
errors['card'] = 'Invalid id'
try:
data['owner_account'] = models.Account.objects.get(pk=request.POST['owner_account'], owner=request.user)
except (ObjectDoesNotExist, KeyError):
if 'owner_account' not in request.POST:
errors['owner_account'] = 'This field is required'
else:
errors['owner_account'] = 'This account does\'t exist or isn\'t yours'
else:
card = self.instance.card
# Check for skill slots
if 'skill_slots' in data and card:
for validator in [MinValueValidator(card.min_skill_slot), MaxValueValidator(card.max_skill_slot)]:
try:
validator(data['skill_slots'])
except DjangoCoreValidationError as e:
errors['skill_slots'] = e.messages
elif card:
data['skill_slots'] = card.min_skill_slot
if errors:
raise serializers.ValidationError(errors)
return data