Last active
August 7, 2023 07:51
-
-
Save antonagestam/d0a0435b8a772d4caa6cf88d9698d44c to your computer and use it in GitHub Desktop.
Django model fields for easily creating fields with min and max validators and that renders with min and max attributes on the input element.
This file contains hidden or 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
| from typing import Dict, Optional, Any | |
| from django.db import models | |
| from django.core.validators import MinValueValidator, MaxValueValidator | |
| from django import forms | |
| class BaseBoundedField(models.Field): | |
| def __init__( | |
| self, | |
| min_value: float = float('-infinity'), | |
| max_value: float = float('infinity'), | |
| step: Optional[float] = None, | |
| *args: Any, **kwargs: Any): | |
| self.max_value = max_value | |
| self.min_value = min_value | |
| self.step = step | |
| kwargs['validators'] = list(kwargs.get('validators', [])) + [ | |
| MinValueValidator(self.min_value), | |
| MaxValueValidator(self.max_value)] | |
| super().__init__(*args, **kwargs) | |
| def _get_widget_attrs(self) -> Dict[str, str]: | |
| attrs = {} | |
| if self.max_value < float('infinity'): | |
| attrs['max'] = str(self.max_value) | |
| if self.min_value > float('-infinity'): | |
| attrs['min'] = str(self.min_value) | |
| if self.step is not None: | |
| attrs['step'] = str(self.step) | |
| return attrs | |
| class BoundedFloatField(BaseBoundedField, models.FloatField): | |
| def formfield(self, **kwargs: Any) -> forms.Field: | |
| defaults = { | |
| 'widget': forms.NumberInput(attrs=self._get_widget_attrs()), | |
| } | |
| defaults.update(kwargs) | |
| return super().formfield(**defaults) | |
| class BoundedIntegerField(BaseBoundedField, models.IntegerField): | |
| def formfield(self, **kwargs: Any) -> forms.Field: | |
| defaults = { | |
| 'widget': forms.NumberInput(attrs=self._get_widget_attrs()), | |
| } | |
| defaults.update(kwargs) | |
| return super().formfield(**defaults) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment