Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class RangedIntegerField(models.IntegerField):
def __init__(self, min_value=None, max_value=None, **kwargs):
self.min_value = min_value
self.max_value = max_value
if 'validators' in kwargs:
validators = kwargs['validators']
else:
validators = []
if min_value:
validators.append(MinValueValidator(min_value))
if max_value:
validators.append(MaxValueValidator(max_value))
kwargs['validators'] = validators
super(RangedIntegerField, self).__init__(**kwargs)
def formfield(self, **kwargs):
context = {'min_value': self.min_value, 'max_value': self.max_value}
context.update(kwargs)
return super(RangedIntegerField, self).formfield(**context)
@sekeiryuutei
Copy link

sekeiryuutei commented Jun 26, 2021

Hola, como lo importo?, muchas gracias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment