本文整理汇总了Python中django.core.validators.MaxValueValidator方法的典型用法代码示例。如果您正苦于以下问题:Python validators.MaxValueValidator方法的具体用法?Python validators.MaxValueValidator怎么用?Python validators.MaxValueValidator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.validators
的用法示例。
在下文中一共展示了validators.MaxValueValidator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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_
示例2: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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_
示例3: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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
)
示例4: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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')
示例5: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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))
示例6: clean
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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: formfield
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [as 别名]
def formfield(self, **kwargs):
defaults = {'min_value': self.min_value, 'max_value':self.max_value}
defaults.update(kwargs)
field = super(FileSizeField, self).formfield(**defaults)
# monkey-patch a better error message
for v in field.validators:
if isinstance(v, MaxValueValidator):
v.message = "We currently limit submissions to %i kB on this system. For larger files, we suggest having students upload the files to their web space and submitting a URL." % (settings.MAX_SUBMISSION_SIZE)
return field
示例8: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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))
示例9: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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
示例10: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [as 别名]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
user_sshkeys = self.created_by.created_sshkeys.all()
self.fields["ssh_key"].queryset = user_sshkeys.all()
self.fields["ssh_key"].help_text = (
"The SSH key to deploy to the cluster. "
'See <a href="%s">your keys</a> or '
'<a href="%s">add a new one</a>.'
% (reverse("keys-list"), reverse("keys-new"))
)
# if the user is not a cluster maintainer, reset the max
# to the default so they can't create larger clusters
if not self.created_by.has_perm("clusters.maintain_cluster"):
max_size = settings.AWS_CONFIG["MAX_CLUSTER_SIZE"]
self.fields["size"].max_value = max_size
self.fields["size"].validators.append(
validators.MaxValueValidator(max_size)
)
self.fields["size"].widget.attrs["max"] = max_size
self.fields["size"].help_text = (
"Number of workers to use in the cluster, between 1 and %s. "
"For testing or development 1 is recommended." % max_size
)
# if there are fewer options we just show radio select buttons
if user_sshkeys.count() <= 6:
self.fields["ssh_key"].widget = forms.RadioSelect(
choices=self.fields["ssh_key"].choices, attrs={"class": "radioset"}
)
示例11: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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))
示例12: max_value_current_year
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [as 别名]
def max_value_current_year(value):
return MaxValueValidator(current_year())(value)
示例13: validators
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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_
示例14: validate
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [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
示例15: __init__
# 需要导入模块: from django.core import validators [as 别名]
# 或者: from django.core.validators import MaxValueValidator [as 别名]
def __init__(self, max_value=None, min_value=None, *args, **kwargs):
self.max_value, self.min_value = max_value, min_value
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))