On Tue, Oct 6, 2009 at 2:49 PM, Aaron <aa...@genieknows.com> wrote: > > Just tried it, like this: > > class MyModel(models.Model): > myfield = models.CharField(unique = True, blank = True, null = > True) > > That can't be right because this field is missing max_length.
> def save(self, force_insert = False, force_update = False): > if self.myfield == '': > self.myfield = None > > super(MyModel, self).save(force_insert, force_update) > > I added: def __unicode__(self): return u'pk %d, myfield=%s' % (self.pk, self.myfield) > I still get integrity errors if I leave myfield blank. :( > > I can't recreate that. I only get IntegrityError for a non-empty-string duplicate value: >>> from ttt.models import MyModel >>> MyModel.objects.create(myfield='') <MyModel: pk 1, myfield=None> >>> MyModel.objects.create(myfield='') <MyModel: pk 2, myfield=None> >>> MyModel.objects.create(myfield='') <MyModel: pk 3, myfield=None> >>> MyModel.objects.create(myfield='x') <MyModel: pk 4, myfield=x> >>> MyModel.objects.create(myfield='x') Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 126, in create return self.get_query_set().create(**kwargs) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 315, in create obj.save(force_insert=True) File "/home/kmt/software/web/playground/ttt/models.py", line 237, in save super(MyModel, self).save(force_insert, force_update) File "/usr/lib/python2.5/site-packages/django/db/models/base.py", line 410, in save self.save_base(force_insert=force_insert, force_update=force_update) File "/usr/lib/python2.5/site-packages/django/db/models/base.py", line 495, in save_base result = manager._insert(values, return_id=update_pk) File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 177, in _insert return insert_query(self.model, values, **kwargs) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 1087, in insert_query return query.execute_sql(return_id) File "/usr/lib/python2.5/site-packages/django/db/models/sql/subqueries.py", line 320, in execute_sql cursor = super(InsertQuery, self).execute_sql(None) File "/usr/lib/python2.5/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql cursor.execute(sql, params) File "/usr/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute return self.cursor.execute(sql, params) File "/usr/lib/python2.5/site-packages/django/db/backends/sqlite3/base.py", line 193, in execute return Database.Cursor.execute(self, query, params) IntegrityError: column myfield is not unique >>> This is with Django 1.1 Karen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---