Tuesday, April 1, 2014

Why can invalid models be saved?

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.

No comments:

Post a Comment