Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this in an overridden <code>save</code> method. The thing to remember is that Django model instances aren't the actual database objects, they just get their values from there on load. So you can easily go back to the database before saving your current object to get the existing values.</p> <pre><code>def save(self, *args, **kwargs): if self.status == 'pending': old_instance = MyClass.objects.get(pk=self.pk) if old_instance.status == 'activated': raise SomeError super(MyModel, self).save(*args, **kwargs) </code></pre> <p>There is currently no good way of returning an error message to the user other than raising an exception. There is a Google Summer of Code project currently under way to enable 'model validation', but this will not be ready for a few months.</p> <p>If you want to do something similar in the admin, the best way is to define a custom ModelForm with an overridden <code>clean()</code> method. However, this time since this is a form you already have access to the old values without hitting the db again. Another benefit is that you can return a form validation error to the user.</p> <pre><code>class MyModelForm(forms.ModelForm): class Meta: model = MyModel def clean_status(self): status = self.cleaned_data.get('status', '') if status == 'pending': if self.instance and self.instance.status == 'activated': raise forms.ValidationError( 'You cannot change activated to pending' ) return status class MyModelAdmin(forms.ModelAdmin): form = MyModelForm model = MyModel </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload