6
Why not take advantage of a ModelForm
? Form data is saved in two steps:
- To the form instance
- To the model instance
So when you have a form:
class YourForm(forms.ModelForm):
class Meta:
model = CustomModel
fields = ['count']
def clean(self):
cleaned_data = super().clean()
count = cleaned_data.get('count')
if count < self.instance.count:
self.add_error('count', 'You cannot decrease the counter')
return cleanded_data
You can then override the form within the django
admin site.
2
There’s also a solution just using the model:
def clean(self):
if self.pk:
previous_count = self.__class__.objects.get(pk=self.pk).count
else:
previous_count = None # If saving a new instance
self.__class__
access the model class and fetch the currently stored .count
value.
- [Django]-How to redirect (including URL change) in Django?
- [Django]-ImportError: cannot import name <model_class>
- [Django]-Django create dates list from queryset
- [Django]-How to use IntegerRangeField in Django template
- [Django]-TypeError: at / 'module' object is not callable
Source:stackexchange.com