Hello community,

I'm using a django version 1.3 beta 1 SVN-15481 and a mysql 5 backend

I have the following field definition in my model:

class Jtem(models.Model):
    name=models.CharField(blank=True, null=True, max_length=255)
    nachname=models.CharField(max_length=255)

Doing manage.py syncdb  results in a table with the following definition:

CREATE TABLE `roman_jtem` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `nachname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci


Now I insert some values in the table so i can run the following:

mysql> select * from roman_jtem where id=29;

+----+------+----------+
| id | name | nachname |
+----+------+----------+
| 29 | NULL | Klesel   |
+----+------+----------+
1 row in set (0.00 sec)

Now I will access the table through django and save an empty string to
the name field:

>>> from test_django.roman.models import Jtem
>>> o=Jtem.objects.get(pk=29)
>>> o.name=''
>>> o.save()

Now the record looks like this:

mysql> select * from roman_jtem where id=29;
+----+------+----------+
| id | name | nachname |
+----+------+----------+
| 29 |      | Klesel   |
+----+------+----------+
1 row in set (0.00 sec)

This is not what I expected! The empty string should have been
converted into a null value!

This behavior causes problems in other models where I access a legacy
database and where I update the model through a ModelForm. Because all
the null Values in the database are replaced with an empty string for
each Model I upddate.

The whole things behaves correctly (to my likings) with the following
patch to the django sources:

diff --git a/django/db/models/fields/__init__.py
b/django/db/models/fields/__init__.py
index fd0a295..569bba6 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -554,6 +554,8 @@ class CharField(Field):
         return smart_unicode(value)

     def get_prep_value(self, value):
+        if self.null and not value:
+            return None
         return self.to_python(value)

     def formfield(self, **kwargs):

Bug or feature?

Regards Roman

-- 
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.

Reply via email to