Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Django has a <a href="https://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects" rel="noreferrer">model validation</a> system in place since version 1.2.</p> <p>In comments sebpiq says "Ok, now there is a place to put model validation ... except that it is run only when using a ModelForm! So the question remains, when it is necessary to ensure that validation is respected at the db-level, what should you do? Where to call full_clean?"</p> <p>It's not possible via Python-level validation to ensure that validation is respected on the db level. The closest is probably to call <code>full_clean</code> in an overridden <code>save</code> method. This isn't done by default, because it means everybody who calls that save method had now better be prepared to catch and handle <code>ValidationError</code>. </p> <p>But even if you do this, someone can still update model instances in bulk using <code>queryset.update()</code>, which will bypass this validation. There is no way Django could implement a reasonably-efficient <code>queryset.update()</code> that could still perform Python-level validation on every updated object.</p> <p>The only way to really guarantee db-level integrity is through db-level constraints; any validation you do through the ORM requires the writer of app code to be aware of when validation is enforced (and handle validation failures).</p> <p>This is why model validation is by default only enforced in <code>ModelForm</code> - because in a ModelForm there is already an obvious way to handle a <code>ValidationError</code>.</p>
    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. 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