9👍
✅
There’s currently no way of performing validation in model save methods. This is however being developed, as a separate model-validation branch, and should be merged into trunk in the next few months.
In the meantime, you need to do the validation at the form level. It’s quite simple to create a ModelForm
subclass with a clean()
method which does your remote call and raises the exception accordingly, and use this both in the admin and as the basis for your other forms.
15👍
Since Django 1.2, this is what I’ve been doing:
class MyModel(models.Model):
<...model fields...>
def clean(self, *args, **kwargs):
if <some constraint not met>:
raise ValidationError('You have not met a constraint!')
super(MyModel, self).clean(*args, **kwargs)
def full_clean(self, *args, **kwargs):
return self.clean(*args, **kwargs)
def save(self, *args, **kwargs):
self.full_clean()
super(MyModel, self).save(*args, **kwargs)
This has the benefit of working both inside and outside of admin.
- Django doesn't create translation .po files
- Django: datetime filter by date ignoring time
- Django: use render_to_response and set cookie
- Where is Pip3 Installing Modules?
- Django query how to write: WHERE field LIKE '10__8__0__'?
Source:stackexchange.com