Hi Toby, you have not read properly the django docs. Note that empty string values will always get stored as empty strings, not as NULL.
only use null=True for non-string fields such as integers, booleans and dates. For both types of fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage. Avoid using null on string-based fields such as CharField and TextField unless you have an excellent reason. null is purely database-related, whereas blank is validation-related. If a field has blank=True, validation on Django’s admin site will allow entry of an empty value. If a field has blank=False, the field will be required. but in your case you want to leave the field with no string then go to back-end and change the value NULL to 'NOT NULL' to that field. On Feb 12, 8:35 pm, tow <toby.o.h.wh...@googlemail.com> wrote: > 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 django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---