Wednesday, December 4, 2013

Re: Complex validation

On 5/12/2013 3:04pm, Lachlan Musicman wrote:
> Hola,
>
> I have an object Part that has a method part_number()
>
> part_number is a function that returns an identifying string based on
> a number of different vars on the model.
>
> part_number needs to be unique, but the building of the part_number is
> complex enough that it cannot just be done via class Meta:
> unique_together
>
> The model's clean():
>
> pn = self.part_number()
> all_parts = Part.objects.all()
> for part in all_pns:
> if pn == part.part_number():
> raise ValidationError('Part numbers must be unique.
> Part number %s already exists.' % part.part_number())

What happens if clean() is called for an existing Part?

>
>
> It's fairly straight forward.
>
> Ideally I would like to make the %s in the error message to an <a>
> link for reference value.

IMHO, "ideally" is a deadly whirlpool which sucks your life away without
measurable gain. I reckon you should aim for the most basic/simple/easy
mechanism. Then later on, if users can actually make a decent case for
extra information via a hyperlink you could add the extra feature.

Good luck

Mike

>
> Is it possible to use/pass context data like that in a ValidationError message?
>
> eg:
>
> raise ValidationError(mark_safe('Part numbers must be unique. Part
> number %s <a href="parts/{{ part.slug }}'>already exists</a>.' %
> part.part_number()))
>
>
> cheers
> L.
>
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52A0297C.8050405%40dewhirst.com.au.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate