我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.core.validators.MinValueValidator()。
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, 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): nb_questions = kwargs.pop('nb_questions') super(CoeffForm, self).__init__(*args, **kwargs) for i in range(1, nb_questions + 1): self.fields['coeff_%s' % i] = forms.\ FloatField(required=True, validators=[MinValueValidator(0)]) self.fields['coeff_%s' % i].label = 'Q%i' % i
def __init__(self, *args, **kwargs): nb_questions = kwargs.pop('nb_questions') super(AddQuestionForm, self).__init__(*args, **kwargs) self.fields['question'] = forms.IntegerField( required=True, validators=[MinValueValidator(1), MaxValueValidator(nb_questions + 1)]) self.fields['question'].label = 'Question number' self.fields['question'].help_text = ('(value between 1 and %s)' % (nb_questions + 1))
def __init__(self, *args, **kwargs): nb_questions = kwargs.pop('nb_questions') super(RemoveQuestionForm, self).__init__(*args, **kwargs) self.fields['question'] = forms.IntegerField( required=True, validators=[MinValueValidator(1), MaxValueValidator(nb_questions)]) self.fields['question'].label = 'Question number' self.fields['question'].help_text = ('(value between 1 and %s)' % (nb_questions))
def __init__(self, *args, **kwargs): if "validators" not in kwargs: kwargs["validators"] = [validators.MinValueValidator(1)] super(p22Min1Field, self).__init__(*args, **kwargs)
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 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])
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, 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
def validators_default_inp(cls): if cls.inp().exists(): return cls.inp()[0].validators return [MinValueValidator(_Zero)]
def validators_default_out(cls): if cls.out().exists(): return cls.out()[0].validators return [MinValueValidator(_Zero)]