I know this seems to be a constant source of confusion; but I seem to
have managed to confuse myself:
class TextData(models.Model):
text = models.TextField(blank=True)
So I have a model with one textfield, whose value can be empty, and I
don't want to worry about distinguishing NULLs and empty strings.
perfect. So far so good according to the documentation.
obj = TextData()
obj.text = ''
obj.save()
... works fine
obj = TextData()
obj.text = None
obj.save()
gives me an IntegrityError. Why? I don't care that whether that's
saved as a Null or an zero-length string, I just want Django to save
back my data according to whatever convention it's using for this
field - the docs tell me if I do blank=True, then it'll use an empty
string and not bother distinguishing nulls; or at least so they imply
to me. But apparently it is trying to distinguish nulls, because it's
given me an IntegrityError. I was expecting Django to coerce the None
to an empty string before saving.
The reason why I'm doing this is because I'm filling up a model object
(which actually contains lots of fields), based upon a dictionary
which might be incomplete.
I really want to do something like:
d = incomplete_dictionary()
obj = MyModel()
for k in fieldnames;
setattr(k, d.get(k))
but I can't; that'll set missing fields to None. What I'm having to do
instead is special-case all of the string values in order to set them
to the empty string instead, which is rather more fragile.
Toby
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---