On 21/03/2016 7:29 PM, Amine Yaiche wrote:
> Hi everyone,
>
> Given a field in a model that uses choices:
>
> |
> CHOICES =(
> Â Â (0,"choice_0"),
> Â Â (1,"choice_1")
> )
>
> foo =models.IntegerField(choices=CHOICES)
> |
>
>
> If i put a value other than 0 or 1, django will accept it. Is that
> possible that we instruct Django to accept only values that are
> mentioned in CHOICES without creating a validator function.
Why don't you want to validate in the model?
I find it valuable to put all sorts of validators in the clean() method
and raise a ValidationError to force the user to enter good data. eg.,
if self.foo not in CHOICES:
raise ValidationError("bad foo")
Unless there are special reasons otherwise, I think it is much better in
the model than the form.
Mike
>
> --
> 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...@googlegroups.com
> <mailto:
django-users...@googlegroups.com>.
> <mailto:
django...@googlegroups.com>.
> <
https://groups.google.com/d/msgid/django-users/78d84aa0-506f-4d5c-9590-ef6325c255e0%40googlegroups.com?utm_medium=email&utm_source=footer>.