我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.core.validators.MaxValueValidator()。
def set_integer(instance, field, min_value=-2**31, max_value=2**31 - 1): """ IntegerField """ possibles = [] possibles.extend(get_field_choices(field)) if possibles: setattr(instance, field.name, random.choice(possibles)) return for validator in field.validators: if isinstance(validator, MinValueValidator): min_value = max(min_value, validator.limit_value) if isinstance(validator, MaxValueValidator): max_value = min(max_value, validator.limit_value) value = random.randint(min_value, max_value) setattr(instance, field.name, value)
def set_float(instance, field): """ FloatField """ possibles = [] possibles.extend(get_field_choices(field)) if possibles: setattr(instance, field.name, random.choice(possibles)) return min_value = -2 ** 8 max_value = 2 ** 8 - 1 for validator in field.validators: if isinstance(validator, MinValueValidator): min_value = max(min_value, validator.limit_value) if isinstance(validator, MaxValueValidator): max_value = min(max_value, validator.limit_value) value = random.uniform(min_value, max_value) setattr(instance, field.name, value)
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))
def __init__(self, request, instance, *args, **kwargs): super(DcNodeSerializer, self).__init__(request, instance, *args, **kwargs) if not kwargs.get('many', False): # Maximum = node resources cpu_n, ram_n, disk_n = instance.node.resources self.fields['cpu'].validators.append(validators.MaxValueValidator(int(cpu_n))) self.fields['ram'].validators.append(validators.MaxValueValidator(int(ram_n))) self.fields['disk'].validators.append(validators.MaxValueValidator(int(disk_n))) if request.method == 'PUT': # Minimum = used resources in this DC (recalculate from node) cpu_min, ram_min, disk_min = instance.node.get_used_resources(request.dc) else: # Minimum = used resources in this DC (DcNode set - DcNode free) cpu_min = (instance.cpu or 0) - instance.cpu_free ram_min = (instance.ram or 0) - instance.ram_free disk_min = (instance.disk or 0) - instance.disk_free self.fields['cpu'].validators.append(validators.MinValueValidator(cpu_min)) self.fields['ram'].validators.append(validators.MinValueValidator(ram_min)) self.fields['disk'].validators.append(validators.MinValueValidator(disk_min))
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')
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_
def __init__(self, user, **kwargs): self.user = user super(AcknowledgeForm, self).__init__(**kwargs) max_acknowledge = self.instance.get_check_instance().get_max_acknowledge() if max_acknowledge: self.fields['days'].validators.append(MaxValueValidator(max_acknowledge))
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))
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
def __init__(self, *args, **kwargs): if "validators" not in kwargs: kwargs["validators"] = [validators.MinValueValidator(2), validators.MaxValueValidator(18), validators.RegexValidator(r"\d{1,2}")] super(DressSizeTypeField, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): if "validators" not in kwargs: kwargs["validators"] = [validators.MinValueValidator(8), validators.MaxValueValidator(12)] super(MediumDressSizeTypeField, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): if "validators" not in kwargs: kwargs["validators"] = [validators.MinValueValidator(2), validators.MaxValueValidator(6), validators.RegexValidator(r"\d{1}")] super(SmallDressSizeTypeField, self).__init__(*args, **kwargs)
def validators(self): range_validators = [] internal_type = self.get_internal_type() min_value, max_value = connection.ops.integer_field_range(internal_type) min_value = min_value or self.min_value if min_value is not None: range_validators.append(MinValueValidator(min_value)) max_value = max_value or self.max_value if max_value is not None: range_validators.append(MaxValueValidator(max_value)) return super(models.IntegerField, self).validators + range_validators
def set_decimal(instance, field, max_digits=10): """ DecimalField """ max_digits = min(max_digits, field.max_digits) min_value = -10 ** max_digits max_value = 10 ** max_digits for validator in field.validators: if isinstance(validator, MinValueValidator): min_value = max(min_value, validator.limit_value) if isinstance(validator, MaxValueValidator): max_value = min(max_value, validator.limit_value) value = random.randint(min_value, max_value) value = Decimal(value) / 10 ** field.decimal_places setattr(instance, field.name, value)
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))
def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value self.max_digits, self.decimal_places = max_digits, decimal_places super(DecimalField, 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))
def __init__(self, attempt, *args, **kwargs): if 'initial' in kwargs: del kwargs['initial'] super().__init__(initial=attempt.__dict__, *args, **kwargs) self.fields['score'].validators.append(validators.MaxValueValidator(attempt.task.max_score)) self.fields['score'].help_text = 'Out of %d' % attempt.task.max_score
def __validators(self): for validator in self.validators: if isinstance(validator, validators.MaxLengthValidator): max_value = 2 ** (validator.limit_value * 8) yield validators.MaxValueValidator(max_value) else: yield validator
def __init__(self, max_value=None, min_value=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value kwargs.setdefault('widget', NumberInput if not kwargs.get('localize') else 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))
def validators(self): v = [] if self.min_amount <= self.min_commission and self.min_commission: v+= [MinValidator(self.min_commission), ] if self.min_amount > self.min_commission and self.min_amount: v += [MinValueValidator(self.min_amount), ] if self.max_amount: v += [MaxValueValidator(self.max_amount), ] return v