Django Models blank=False Problem

6 views
Skip to first unread message

madhav

unread,
Feb 19, 2009, 7:45:35 AM2/19/09
to Django users
I have a model something like this:
class CandidateEmailMessage(models.Model):
from_email = models.EmailField(max_length=100)
to_email = models.EmailField(max_length=100)
body = models.TextField()
objects = CandidateEmailMessageManager()

As the django documentation says blank=False,null=False are there by
default. So if I do
candidate_email_object = CandidateEmailMessage
(from_email='',to_email='',body='');
candidate_email_object.save()
it should not be saved. But its actually saving, an empty object. I am
running the above script in my test case. How is it actually allowing
that?

Karen Tracey

unread,
Feb 19, 2009, 9:26:49 AM2/19/09
to django...@googlegroups.com

Currently validation is only being done at the form level.  If you created a ModelForm for this model and tried to specify blanks for those fields the form's is_valid() method would return False and the error messages would indicate that values were required for those fields.  But you've not used a form, and we don't yet have model validation, so right now you don't get an error. 

You might find this post by Malcolm useful if you want to do some validation like this in the absence of built-in model validation in Django:

http://www.pointy-stick.com/blog/2008/10/15/django-tip-poor-mans-model-validation/

Karen
Reply all
Reply to author
Forward
0 new messages