views:

36

answers:

1

I have a model (and model form based on it) that has several time fields. I want these fields to be optional such that the user may leave some empty. My problem is that I continue to receive "Column 'mechreturn_tm' cannot be null" errors when I attempt to save an instance where one or more of these fields are blank. I've used the following approaches:

mechreturn_tm = models.TimeField(blank=True, null=False)
mechreturn_tm = models.TimeField(blank=True, null=True)
mechreturn_tm = models.TimeField()

none seem to work. what am I doing wrong?

A: 

Are you sure you still get the error using the second approach (both blank and null set to to True)?

Keep in mind that you'll need to recreate the DB table when you make this change since syncdb won't alter an existing table. That may be why you got the error even with the correct code.

Setting blank=True on a model field means that the field is allowed to be blank when validating a form with that field.

Setting null=True means that the model can be saved to the DB without any value at all for that field.

http://docs.djangoproject.com/en/dev/topics/db/models/#field-options

John Debs
Thanks John and tcarobruce. I think this is the problem as I've been trying syncdb after each change without first deleting the tables. Will delete then recreate the tables with the altered code.
kjarsenal
Thanks again. mechreturn_tm = models.TimeField(blank=True, null=True) worked fine.
kjarsenal