Great proposal and I'd love it on forms. I'm not sure about models, though. 
I always use forms for sanitization. 

On Wednesday, 25 March 2015 10:37:43 UTC+1, guettli wrote:
>
> Up to now the validation/cleaning of a attribute can not access the 
> value of an other attribute. 
>
> The work around is to override the clean() method of the form/model. 
>
> A team mate and I talked about it, and we asked ourself, if 
> a two phase cleaning would help here. 
>
> Our idea: 
>
>   - The first clean phase works like the current implementation 
>
>   - The second clean phase is optional and does nothing except the user 
> implements it. 
>
>   - In the second clean phase your are allowed to peek into 
>     the results of the first clean phase. 
>     The clean method for the second phase would need to get the other 
> values passed in. 
>
>
> I would like to have this on model level, not only for forms. 
>
> Example: 
>
> class Issue(models.Model): 
>      state=models.CharField(choices=[('in_progress', 'In progress'), 
> ('wait_until_due_date', 'Wait until due date')]) 
>      due_date=models.DateField(null=True) 
>
>
> We want to implement this constraint: 
>
>   - due date must be set, if the state is "wait_until_due_date". 
>   - due date must not be set, if state is different from 
> "wait_until_due_date". 
>
> We know that we can use the clean() method of the model, but a field based 
> solution would 
> be more preferred. 
>
> class Issue(models.Model): 
>      state = models.CharField(choices=[('in_progress', 'In progress'), 
> ('wait_until_due_date', 'Wait until due date')]) 
>      due_date = models.DateField(null=True, 
> validators2=[check_state_and_due_date_match]) 
>
> def check_state_and_due_date_match(due_date, all_fields): 
>      state = all_fields['state'] 
>      if due_date: 
>          if state != 'wait_until_due_date': 
>              raise ValidationError('If due date is set, state must be 
> "wait_until_due_date"') 
>          return 
>      if  state != 'wait_until_due_date': 
>          raise  ValidationError('If no due date is set, state must be 
> "wait_until_due_date") 
>
>
> Open questions: I am unsure if this should be used only for validation, or 
> if it should be possible 
> to alter values, too. 
>
> What do you think? 
>
> Other solutions or improvements? 
>
> Regards, 
>    Thomas Güttler 
>
>
>
> -- 
> Thomas Güttler 
> http://thomas-guettler.de/ 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@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/e499191b-1b18-4c08-b097-48b689ae585c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to