On 23/01/2014 10:23pm, parnigot wrote:
Thanks Mike,
Il giorno 23/gen/2014, alle ore 09:16, Mike Dewhirst
<mi...@dewhirst.com.au <mailto:mi...@dewhirst.com.au>> ha scritto:
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.
What about the MinValueValidator and the custom field. Do they validate
automatically only with forms like model.clean() or they raise
ValidationError even with a model.save()?
* 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.
Ok, thanks for reminding me that. But my original question was in which
method of my PositiveCustomField I need to put the logic to check if the
value is valid:
Honestly, I have only ever used one custom field and eventually ripped
it out because the non-standard-ness added to the maintenance load.
So I'm not the right person to answer that one ...
Sorry
Mike
if value <= 0:
raise ValidationError(“value must be > 0”)
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+unsubscr...@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/C1F789C3-0CE8-4333-9465-0255234A6EAD%40gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
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+unsubscr...@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/52E105C3.5070000%40dewhirst.com.au.
For more options, visit https://groups.google.com/groups/opt_out.