Changes between Initial Version and Version 2 of Ticket #29549


Ignore:
Timestamp:
Jul 6, 2018, 2:01:19 PM (6 years ago)
Author:
Evgeny Arshinov
Comment:

Replying to Tim Graham:

Did you mean to title the ticket "Model.save() doesn't validate CHOICES"?

Yes. Sorry for the incorrect title, I fixed that.

This is documented:

Note, however, that like Model.full_clean(), a model’s clean() method is not invoked when you call your model’s save() method.

OK, I think I did not pay enough attention to the point that saving and validation are two different steps, as documented in the model instance reference, and fields are generally validated during clean() except for explicitly documented cases like unique (which is validated in save()).

However, it seems that many people stumble upon this issue. Maybe an explicit note could be added to the documentation for the choices parameter?

This is because of backwards compatibility (and probably performance considerations).

There are ways to change the behavior in your project.

OK, thank you for the direct link. We will consider using this approach in our project.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #29549

    • Property Summary ModelForms doesn't validate CHOICESModel.save() doesn't validate CHOICES
  • Ticket #29549 – Description

    initial v2  
    55As a developer, I would expect `model_instance.save()` to validate the `choices` field against the list of possible choices, not least because the list is defined at the model level, participates in db migrations etc., so it should take effect on modal instances.
    66
    7 I haven't been able to find any documentation or existing bug reports that clearly state that [choices https://docs.djangoproject.com/en/2.0/ref/models/fields/#choices] argument only affects the corresponding model form field presentation and does not ensure data integrity.
     7I haven't been able to find any documentation or existing bug reports that clearly state that [https://docs.djangoproject.com/en/2.0/ref/models/fields/#choices choices] argument only affects the corresponding model form field presentation and does not ensure data integrity.
    88
    99In summary:
Back to Top