Created
February 21, 2019 16:59
-
-
Save madneon/147159f46ed478c71d5ee4950a9d697d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola, como lo importo?, muchas gracias