Tuesday, April 1, 2014

Re: Why can invalid models be saved?

Most validation is in the processing of HTML forms, to help the site user (or admin) avoid errors.  Checking on the way to the database is much more limited, and is usually limited to the ability to coerce the field to the correct type.  Since django works with many databases, there is very little common ground for specifying that the database itself check, maybe just not null.

A programmer, using the models directly (that's the presumption if you're typing python statements) is expected to know what he's doing.

Bill


On Tue, Apr 1, 2014 at 4:19 AM, Claus Conrad <lists@clausconrad.com> wrote:
Hi,

sorry for the newbie question!

In the admin I cannot create a user without a username, but on the shell it is possible:

>>> from django.contrib.auth.models import User
>>> u = User()
>>> u.save()
>>> u.id
2

I am trying to understand why this is possible. Does this mean model constraints in general are not enforced when instances are created and saved? Thus I will always have to validate my models before saving them, unless I set null=True on a field, in which case I'd expect to get an exception?

Thanks,
Claus

--
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/4b097bd9-cc8b-4511-8efa-2fb4537b702f%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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB%2BAj0tMPgCfa4vk5Fib19tanNjMrGV88yuS61%2B3gp0kpykY-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment