List Info

Thread: Can't default CharField to empty string?




Can't default CharField to empty string?
user name
2006-12-29 21:52:00
Hi --

I know there's a lot of confusion over empty strings and
NULLs, and
I've read a bunch of the threads about them, but I can't
seem to find a
solution to this problem:

Basically, I want to be able to leave a field blank in the
admin, and
have it insert an empty string instead of a NULL into the
database (so
that the field can be set to NOT NULL and PosgreSQL won't
barf).

The docs seem to imply that Django will do this, but I can't
get it to.

-----------
If True, Django will store empty values as NULL in the
database.
Default is False.

Note that empty string values will always get stored as
empty strings,
not as NULL -- so use null=True for non-string fields such
as integers,
booleans and dates.
------------

Here's my field definition:

notes = models.CharField(maxlength=200, null=False,
default='',
blank=True)

However, every time I try to use the admin to add a record
where the
"notes" field is empty, I get an error from the
database about
inserting a NULL value.  Why doesn't it use the specified
default?  Do
I have to override the save() method or something?

Thanks.


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Can't default CharField to empty string?
user name
2006-12-30 00:15:37
Update: Oops, the character field is working fine.  It's an
"auto_now_add" date field that's causing problems
when editing an
existing item inline with another model in admin.  Here's
the
problematic date field:

submitted = models.DateTimeField('Date Submitted',
auto_now_add=True)

And the SQL error:

ERROR: null value in column "submitted" violates
not-null constraint
UPDATE "myapp_message" SET message"='Mauris
nec mi eget turpis
tincidunt
sodales',notes='',"user_id"='1',"submitted&qu
ot;=NULL WHERE
"id"='30'


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )