Thursday, November 26, 2009

Re: string-based fields and null

On Thu, Nov 26, 2009 at 2:25 PM, chefsmart <moran.corsair@gmail.com> wrote:
Those are good points. I am inclined to think the Django developers
think along the same lines (that is why they suggest to "Avoid using
null on string-based fields such as CharField and TextField unless you
have an excellent reason")

Thus I am also inclined to think that CharField, ImageField etc would
be treating an empty string "" and a database NULL as same in their
code. I am trying to locate that code to know for sure, but haven't
been able to yet.

Regards.


I wouldn't have thought that would be true. Unless you specify 'null=True' on a CharField, django will do its best to stop you ending up with a NULL value in the database. Eg, in MySQL it will create the field with the modifier 'NOT NULL'.

Empty string and NULL/None are different values, both in the database and django. By disallowing NULLs, you just get a more consistent definition of empty string.

For example, one of my models has an ImageField defined as:

  logo = models.ImageField( upload_to="uploads/logos", blank=True )

The db field that this corresponds to looks like this in MySQL (hope you are using a proportional font):
+----------------------+--------------+------+-----+---------+----------------+
| Field                | Type         | Null | Key | Default | Extra          |
+----------------------+--------------+------+-----+---------+----------------+
| logo                 | varchar(100) | NO   |     | NULL    |                |

If I look at a model that has a blank logo field, then it looks like so:

>>> m.logo
<ImageFieldFile: None>

If you are interested in knowing how ImageFields behave, perhaps try creating a few models with ImageFields, with various options and test how they behave? Might be a bit more promising than speculating.

Cheers

Tom

--

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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate