django-mptt/django-mptt — Issue #151: If you use charfield primary key, django-admin has some problems
Author: GMLudo
Created: 2011-10-26 12:33:37+00:00
State: open

Please read the #24 issue.

Contrary to #24, when I create a new item, django-mptt works correctly in manage.py shell.
But, with django-admin, I've the same backtrace as #24.

I've installed the latest dev version: pip2 install git+git://github.com/django-mptt/django-mptt.git

**models.py**:

```python
class Department(MPTTModel):
accountcode = models.CharField(primary_key=True, max_length=20)
name = models.CharField(max_length=80)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

class MPTTMeta:
order_insertion_by = ['name']

def __unicode__(self):
return self.name
```

**admin.py**:

```python
class DepartmentAdmin(admin.ModelAdmin):
list_display = ('accountcode', 'name', 'parent')
list_filter = ['accountcode', 'name', 'parent']
search_fields = ('accountcode', 'name')

admin.site.register(Department, DepartmentAdmin)
```

**Backtrace**:

```python
Environment:

Request Method: POST
Request URL: http://localhost:8000/admin/cdr/department/add/

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['admin_tools',
'admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.markup',
'django.contrib.admindocs',
'mptt',
'cdr',
'dateutil',
'uni_form']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'pagination.middleware.PaginationMiddleware',
'common.filter_persist_middleware.FilterPersistMiddleware')

Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
307. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
197. return view(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
28. return bound_func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
24. return func(self, *args2, **kwargs2)
File "/usr/lib/python2.7/site-packages/django/db/transaction.py" in inner
217. res = func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
882. self.save_model(request, new_object, form, change=False)
File "/usr/lib/python2.7/site-packages/django/contrib/admin/options.py" in save_model
665. obj.save()
File "/usr/lib/python2.7/site-packages/mptt/models.py" in save
614. super(MPTTModel, self).save(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py" in save
460. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py" in save_base
553. result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
195. return insert_query(self.model, values, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
1436. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
791. cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
735. cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/util.py" in execute
34. return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
86. return self.cursor.execute(query, args)
File "/usr/lib/python2.7/site-packages/MySQLdb/cursors.py" in execute
174. self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/site-packages/MySQLdb/connections.py" in defaulterrorhandler
36. raise errorclass, errorvalue

Exception Type: IntegrityError at /admin/cdr/department/add/
Exception Value: (1048, "Column 'lft' cannot be null")
```