Hey Gary, On Thu, 2006-09-14 at 02:09 +0000, Gary Wilson wrote: [...] > Ok, after some trial and error, I see that if you have null=True on a > CharField, it will use NULL instead of empty string. This is what I > needed because in my model I have a field that I want to be optional > but unique. This is not possible if you don't put null=True for the > field because Django will try to use empty string by default. > > Out of curiosity, any one know for what reason using empty strings for > CharFields is the Django convention? Technically, isn't an empty > string still data? Isn't it a bit confusing that some fields get > default values and others do not? Explicit better than implicit?
Looking at this from a database persepctive, it's bad, I agree (NULLs and empty strings are different animals and they should be able to co-exist). However, looking at it from a web framework perspective, a decision has to be made, so Django's approach is not illogical. To explain... Think about how the data is entered: through a web form (the admin interface, or a custom form). Now, how do you differentiate between an explicit empty string and a "no data entered and so it should be NULL" field? With web entry, you can't, so Django chooses to default to using empty strings everywhere. When I first started using Django, this drove me nuts. Why were the developers being so righteous about good web practices and then paying no heed to good databases in this fashion (I thought)? But, when you think about it, it's a hard problem -- differentiating between the two cases for web entry is tough. So the compromise is not unreasonable. Remembering my own reaction, though, I can certainly sympathise with the "are they evil?" thoughts that might occasionally flit through peoples' heads when encountering this. NULLs are still allowed in character fields, so legacy data is supported. Evilness is averted. :-) Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers -~----------~----~----~----~------~----~------~--~---