On 23/01/2014 7:03pm, parnigot wrote:
> Hi,
>
> In my models I’ve some FloatFields that must be > 0. I’m wondering what
> is the best way to check, at the model level (not form), that the values
> for these Fields are greater than 0? Browsing the documentation I’ve
> found the following three methods:
>
> * Add the MinValueValidator
> <
https://docs.djangoproject.com/en/1.6/ref/validators/#minvaluevalidator> to
> each field.
> * Check them in the model.clean() method
I use the model's clean() method which is always called by the form.
However, if you are not using forms you have to remember to call the
clean method. Eg, in unit tests.
> * Create a custom field that accept only floats > 0.
>
>
> And, if a choose the custom field, which FloatField’s method do I need
> to override to check that the value is > 0?
I don't use floats as a rule but I would probably use isinstance and
possibly a range like > -0.000001 and < 0.000001 or whatever your app
requires.
mike
>
> Best regards,
> e.p.
>
> --
> 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.
> To post to this group, send email to
django...@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/DDD1702B-CA41-4A8B-94DC-76C8D6B39EDD%40gmail.com.
> For more options, visit
https://groups.google.com/groups/opt_out.