Hi Thomas,

On 03/25/2015 03:37 AM, Thomas Güttler 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.

Why?

> 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?

I think this is unnecessary complexity to add to the validation process.
There is nothing that this proposal makes possible that isn't already
possible using the clean() method. In cases where the clean() method
becomes too long or convoluted, you can break it up into smaller methods
yourself.

In fact, I would guess that your proposal could be entirely implemented
on top of current Django, by overriding the clean() method to implement
what you've outlined above.

Carl

-- 
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/5512DE42.8000800%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to