class ValidatedModel(models.Model):def save(self, validate=True, *args, **kwargs):if validate:self.full_clean()super(ValidatedModel, self).save(*args, **kwargs)class Meta:abstract = True
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/fa2c2936-4e44-4132-863c-e93c3ee7da8a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hello,I am trying to understand why field validators (model.full_clean()) are not called for model.save()I've spent about an hour reviewing all the discussions/replies on this subject;From what I can tell this was rejected on the basis of breaking backwards compatibility.It is unclear whether this would have been desired behavior had backwards compatibility not been an issue.It also seems to me that it would be possible to maintain backwards compatibility by using settings flags to determine the default behavior, albeit at the cost of increased complexity.So I have four questions;1) Without taking backwards compatibility into consideration, is it reasonable to expect field validation automatically upon calling model.save()
2) At what point would Django devs be willing to break backwards compatibility? Would this be considered on 2.x, or is it life time?
3) Could backwards compatibility not be maintained by means of a flag in settings? For example, settings.MODEL_VALIDATE_ON_SAVE=True would cause the validation to be handled using the new approach?
4) Assuming this cannot/will not be implemented, what would be the most suitable workaround? Are there any risks in calling full_clean() without ever using ModelForm? Should ModelForm always be used instead of using a model directly? etc.
Sorry I should have made myself a little more clear.When I said "invalidating data inside your models" I was referring to any previous data that was saved when the validator was set to a minimum length of 4 characters. By then changing the validator to be 5 characters, you are effectively breaking save() on any data that was saved before that point, and the ability to fix the model data may not be available in that specific save() context. For example, a cron job that updates a "last_checked" field on the user would not necessarily have data relevant to changes in the users phone number (which could have its min/max length changed at any point in the validators life time).Hope that makes more sense
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJxq848-%3DwSerY4HT9zAcff9YVtjQLm7Xf6EmA_JhF3MX7xFkA%40mail.gmail.com.
A few cents...
First, a project wide settings to control this behaviour would be complex with pluggable apps. Some apps might expect global valodation enabled, but some might expect it disabled.
We should have model.save validating by default and have a flag to turn it off. I think it's reasonable to assume that by default we want our data to be consistent.
Regards,
Luciano Pacheco
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAHKQagFR811sPZPyvKgqDUndJx-rAVFHWT6O8N4kpZFBKxsXZg%40mail.gmail.com.
1) Without taking backwards compatibility into consideration, is it reasonable to expect field validation automatically upon calling model.save()Yes. Based on the original discussions (as I recall them), this wasn't in question -- the problem was entirely backwards compatibility.
I think it's reasonable to assume that by default we want our data to be consistent.
Wasn't there also concern for double validation performed during form clean and then model instance save?
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/31648978-7105-422f-ad62-68943e324a04%40googlegroups.com.
I think Anssi summed up my feelings on this very well, in particular with the issues with update(). Similarly create() would also bypass as it does not call save() AFAIK.
Given these commonly used approaches do not work, and that the name of the method - save() - implies only saving to the database - I'm -1 on save() calling clean().
That said, there is an interesting option to explore in ModelForms as to how to push more if the validation to the database where that's appropriate. This would increase the performance of write-heavy sites, and also enforce better data integrity. At the moment all cleaning is done before the instance is saved, and the assumption is that the instance will pass db validation. As far as I know, we don't currently have any code which detects problems there (dB integrity errors) and ties them back to the fields or instances which have caused the issue. This is obviously backend-specific code, and is far from straightforward. I think some of the pending ValidationError changes could make it possible.
Anssi and Andrew - do you think the dB can actually tell is enough information to get this right? I guess if not an option would be to fall back to the current query-creating code to work out why the save failed.
Most of this is throwing thoughts around, apologies if they're illogical…
Marc
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAHKQagFULsc%2BHRKxnR2i_4KZJjLNuPD8aHnbwnf1GjhzduopVg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMwjO1GYQHbAs2PAZHuYke%3DbJY7nNkXb%2BEbDb_9Ek3t2g%3D-ncA%40mail.gmail.com.
The way I dealt with this was creating a mixin (ValidateModelMixin) and adding it as left-most superclass in all the models where I needed 'full_clean' to be called before 'save'.