Skip to content
Advertisement

Django: Get previous value in clean() method

I have a model CustomModel with an IntegerField.

JavaScript

When I create a new instance of CustomModel in the admin, I have to do validation, so I use the clean method and have access to the value with.

JavaScript

My problem:

When I change the instance of CustomModel, I only have access to the new, changed value but not to the original value. However, for my validation I have to compare the new value and the value before the instance got edited.

I could not found a solution how to get access. Does somebody know?

Advertisement

Answer

Why not take advantage of a ModelForm? Form data is saved in two steps:

  1. To the form instance
  2. To the model instance

So when you have a form:

JavaScript

You can then override the form within the django admin site.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement