blank=True isn't working

113 views
Skip to first unread message

Michael J

unread,
Aug 12, 2008, 5:54:16 PM8/12/08
to Django users
I'm guessing I'm overlooking something really simple here, but after
having spent the last hour on this issue, I can't figure out what to
do.

I have a field in my model like so:
about = models.TextField(blank=True)

When I run syncdb the SQL that gets printed out is:
`about` longtext NOT NULL

In the Admin, I can get away with not entering anything for this
field, but if I try and do the same thing in the app. using
create_update.update_object generic view, I get an error saying the
column cannot be left blank!

Here's someone else who posted about this, but no one answered:
http://groups.google.com/group/django-users/browse_thread/thread/edb91189f4adea01/e058171663f6b728?lnk=gst&q=column+cannot+be+blank#e058171663f6b728

Any help is greatly appreciated.

Chris Pickett

unread,
Aug 12, 2008, 6:58:02 PM8/12/08
to django...@googlegroups.com
You also need null=True. Blank tells the admin to allow a blank, null tells the database to allow a "blank".

Chris

Michael J

unread,
Aug 12, 2008, 7:44:48 PM8/12/08
to Django users
Thanks for that. But the reason why I wasn't using null=True is
because of what's in the documenation:

"Avoid using null on string-based fields such as CharField and
TextField unless you have an excellent reason. If a string-based field
has null=True, that means it has two possible values for “no data”:
NULL, and the empty string. In most cases, it’s redundant to have two
possible values for “no data;” Django convention is to use the empty
string, not NULL."

On Aug 12, 5:58 pm, Chris Pickett <cpick...@zodas.com> wrote:
> You also need null=True. Blank tells the admin to allow a blank, null tells the database to allow a "blank".
>
> Chris
>
> ----- Original Message -----
> From: "Michael J" <gagabano...@gmail.com>
> To: "Django users" <django...@googlegroups.com>
> Sent: Tuesday, August 12, 2008 5:54:16 PM GMT -05:00 US/Canada Eastern
> Subject: blank=True isn't working
>
> I'm guessing I'm overlooking something really simple here, but after
> having spent the last hour on this issue, I can't figure out what to
> do.
>
> I have a field in my model like so:
> about = models.TextField(blank=True)
>
> When I run syncdb the SQL that gets printed out is:
> `about` longtext NOT NULL
>
> In the Admin, I can get away with not entering anything for this
> field, but if I try and do the same thing in the app. using
> create_update.update_object generic view, I get an error saying the
> column cannot be left blank!
>
> Here's someone else who posted about this, but no one answered:http://groups.google.com/group/django-users/browse_thread/thread/edb9...

Robert

unread,
Aug 12, 2008, 10:48:12 PM8/12/08
to Django users
Required=False might be a better alternative

Kenneth Gonsalves

unread,
Aug 12, 2008, 11:32:10 PM8/12/08
to django...@googlegroups.com

On 13-Aug-08, at 3:24 AM, Michael J wrote:

> I have a field in my model like so:
> about = models.TextField(blank=True)

try blank=True,null=True

--

regards
kg
http://lawgon.livejournal.com
http://nrcfosshelpline.in/code/

James Bennett

unread,
Aug 12, 2008, 11:56:53 PM8/12/08
to django...@googlegroups.com
On Tue, Aug 12, 2008 at 4:54 PM, Michael J <gagab...@gmail.com> wrote:
> In the Admin, I can get away with not entering anything for this
> field, but if I try and do the same thing in the app. using
> create_update.update_object generic view, I get an error saying the
> column cannot be left blank!

There may be some tricky stuff not being handled by the view here,
which warrants some testing and possibly a bug report.

The docs are correct in that you shouldn't set null=True on a
text-based field; the corollary to this is that when there's no value
for that field, you must insert an empty string. If you try this in
Python code you'll see the difference: trying it with "about=''" will
work, but "about=None", or leaving that field out entirely, won't.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

bruno desthuilliers

unread,
Aug 13, 2008, 9:56:28 AM8/13/08
to Django users


On 13 août, 01:44, Michael J <gagabano...@gmail.com> wrote:
> Thanks for that. But the reason why I wasn't using null=True is
> because of what's in the documenation:
>
> "Avoid using null on string-based fields such as CharField and
> TextField unless you have an excellent reason. If a string-based field
> has null=True, that means it has two possible values for “no data”:
> NULL, and the empty string. In most cases, it’s redundant to have two
> possible values for “no data;” Django convention is to use the empty
> string, not NULL."
>

A very sensible advice. Now for the howto:

class MyModel(models.Model):
value = models.CharField(max_length=42, blank=True, default='')

HTH

Michael J

unread,
Aug 13, 2008, 2:42:31 PM8/13/08
to Django users
No go bruno. That gives me same error.


On Aug 13, 8:56 am, bruno desthuilliers

Michael J

unread,
Aug 13, 2008, 2:43:50 PM8/13/08
to Django users
James,

So what is one to do in my case? Go with about="" each time there's no
value submitted?

On Aug 12, 10:56 pm, "James Bennett" <ubernost...@gmail.com> wrote:

Michael J

unread,
Aug 13, 2008, 2:48:57 PM8/13/08
to Django users
Silly me. That's precisely what you said James. :)

bruno desthuilliers

unread,
Aug 14, 2008, 8:09:30 AM8/14/08
to Django users


On 13 août, 20:42, Michael J <gagabano...@gmail.com> wrote:
> No go bruno. That gives me same error.
>
Mmm ??? I have this very same pattern in dozen places in my code, and
it works just fine.

julianb

unread,
Aug 14, 2008, 9:08:06 AM8/14/08
to Django users
On Aug 13, 8:43 pm, Michael J <gagabano...@gmail.com> wrote:
> James,
>
> So what is one to do in my case? Go with about="" each time there's no
> value submitted?

Use default="" in the model.
Reply all
Reply to author
Forward
0 new messages