Monday, May 30, 2016

Re: Choice fields not validated while saving to database



On 30.05.2016 13:33, Vinayak Kaniyarakkal wrote:


On Monday, 30 May 2016 15:24:57 UTC+5:30, Jani Tiainen wrote:
Django doesn't validate models automatically. You can validate model manually as per docs: 
<https://docs.djangoproject.com/en/1.9/ref/models/instances/#validating-objects>
 
If we put unique=True,  django validate that the field doesn't repeat.
If we don't put null=True, django validate that the field is filled.
So isn't it good to validate that the value is one of the available choices?


Actually Django doesn't validate those either. It's your DBMS that does validate them by setting unique index and setting field not null.

So Django doesn't validate those either - unless you validate them explicitly as documentation states.


On 30.05.2016 12:38, Vinayak Kaniyarakkal wrote:
The following code is "failing silently"
 
class MyModel(models.Model):
    CHOICES = ((0, 'Inactive'), (1, 'Active'),)
    my_field = models.PositiveIntegerField(choices=ACTIVE_CHOICES, default=2)

obj = MyModel.objects.all()[0]
obj.my_field=100
obj.save()

Should I raise a bug in Django?
 
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a073eaca-0e92-4685-ae93-113fc8f0840e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/99054121-7498-4ee5-9099-f63fa7bb3cba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment