On Friday 07 July 2006 12:57, Malcolm Tredinnick wrote:
> Maybe there is some oddity about your circumstances. Can you post more
> details if you are really seeing NULL values being treated distinctly,
> please?

Actually, it may not been SQLite related, after all. I narrowed the problem to 
the way the admin interface create empty fields in the database. Those NULL 
values are not really NULL.

Here's my model:
-------------------------------------------------------------------------------
class Foo(models.Model):
    bar = models.CharField(maxlength = 63, unique=True, blank=True, null=True)

    def __str__(self):
        return str(self.bar)

    class Admin:
        pass
-------------------------------------------------------------------------------

Creating several Foo objects with no bar property works correctly in python 
interpreter:
-------------------------------------------------------------------------------
>>> Foo(bar='bar').save()
>>> Foo().save()
>>> Foo().save()
>>> Foo.objects.all()
[<Foo: bar>, <Foo: None>, <Foo: None>]
-------------------------------------------------------------------------------
That's ok.
And moreover, if I try to create an other Foo with bar='bar', I get 
the 'column bar is not unique' IntegrityError exception.


But if I try the same in the admin interface, I get:
-------------------------------------------------------------------------------
Traceback (most recent call last):
 File "/home/kilian/django/django/core/handlers/base.py" in get_response
   74. response = callback(request, *callback_args, **callback_kwargs)
 File "/home/kilian/django/django/contrib/admin/views/decorators.py" in 
_checklogin
   55. return view_func(request, *args, **kwargs)
 File "/home/kilian/django/django/views/decorators/cache.py" in 
_wrapped_view_func
   40. response = view_func(request, *args, **kwargs)
 File "/home/kilian/django/django/contrib/admin/views/main.py" in add_stage
   257. new_object = manipulator.save(new_data)
 File "/home/kilian/django/django/db/models/manipulators.py" in save
   101. new_object.save()
 File "/home/kilian/django/django/db/models/base.py" in save
   204. ','.join(placeholders)), db_values)
 File "/home/kilian/django/django/db/backends/util.py" in execute
   12. return self.cursor.execute(sql, params)
 File "/home/kilian/django/django/db/backends/sqlite3/base.py" in execute
   77. return Database.Cursor.execute(self, query, params)
 -------------------------------------------------------------------------------
   IntegrityError at /admin/test/foo/add/
   column bar is not unique
/home/kilian/django/django/db/backends/sqlite3/base.py in execute 
        return Database.Cursor.execute(self, query, params) ...
▼ Local vars 
Variable
Value
params
  ['']
  query
  'INSERT INTO "test_foo" ("bar") VALUES (?)'
  self
  <django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0xb5f62c2c>
-------------------------------------------------------------------------------

It seems that blank admin fields are passed as empty strings rather than real 
NULL values. In the command-line, it would give something like:
-------------------------------------------------------------------------------
>>> Foo(bar='').save()
>>> Foo(bar='').save()
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/home/kilian/django/django/db/models/base.py", line 204, in save
    ','.join(placeholders)), db_values)
  File "/home/kilian/django/django/db/backends/util.py", line 12, in execute
    return self.cursor.execute(sql, params)
  File "/home/kilian/django/django/db/backends/sqlite3/base.py", line 77, in 
execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: column bar is not unique
-------------------------------------------------------------------------------

Frommy point of view, the admin interface should probably create db records 
with NULL values for fields left blank in the interface, if (and only if?) 
null=True *and* blank=True. 
If blank=True and null=False, empty admin fields should indeed be stored as 
empty strings (or whatever the db field was defined to). And I can't find any 
example where the (blank=False and null=True) case could be used.

Perhaps there are other situations I didn't think of where this is not the 
right thing to do, I don't know.

Best regards,
-- 
Kilian CAVALOTTI                      Administrateur réseaux et systèmes
UPMC / CNRS - LIP6 (C870)
8, rue du Capitaine Scott                          Tel. : 01 44 27 88 54
75015 Paris - France                               Fax. : 01 44 27 70 00

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to