public
Last active

  • Download Gist
UUID pk MultiValueDictKeyError
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# the bug is described here http://code.djangoproject.com/ticket/12235
# I tried to use the django-extensions implementation of UUIDField(), I have the same bug
# opened this issue for django-extensions
# https://github.com/django-extensions/django-extensions/issues#issue/50
#
# I'm new to django, I just suspect that there is some code in django-admin inlines which
# assumes integer primary keys
 
# using : django 1.2.3 , django-extensions 0.5, PGSQL db with psycopg2
 
# ==============
# in settings.py
# ==============
 
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django_extensions',
'myapp',
)
 
# ==================
# in myapp/models.py
# ==================
 
from django.db import models
from django.db.models import CharField
from django_extensions.db.fields import UUIDField
 
try:
import uuid
except ImportError:
from django_extensions.utils import uuid
 
class Worker(models.Model):
id = UUIDField(primary_key=True)
other_uuid_not_pk = UUIDField()
name = models.CharField(blank=True, max_length=100)
def __unicode__(self):
return(self.name)
 
class Task(models.Model):
id = UUIDField(primary_key=True)
other_uuid_not_pk = UUIDField()
description = models.CharField(blank=True, max_length=100)
workers = models.ManyToManyField(Worker, through="Todo")
def __unicode__(self):
return(self.description)
 
class Todo(models.Model):
id = UUIDField(primary_key=True)
other_uuid_not_pk = UUIDField()
worker = models.ForeignKey(Worker)
task = models.ForeignKey(Task)
time_allowed = models.IntegerField(default=1)
 
# =================
# in myapp/admin.py
# =================
 
from django.contrib import admin
from myapp.models import *
 
class WorkerAdmin(admin.ModelAdmin):
pass
 
class TodoInline(admin.TabularInline):
model = Todo
fk_name = 'task'
raw_id_fields = ('worker',)
extra = 1
class TaskAdmin(admin.ModelAdmin):
inlines = (TodoInline,)
 
admin.site.register(Worker,WorkerAdmin)
admin.site.register(Task,TaskAdmin)
 
# Once synced and admin urls setup, foillow these steps to reproduce the bug :
# 1. Log into admin, add some workers
# 2. Go to Task admin, add a task, add a related worker to it, save
# 3. Go back to this same task admin page, and just try to save again, or add a worker and save, you got :
#
# MultiValueDictKeyError at /admin/myapp/task/c64af3b5-f6e4-11df-869c-001cb3bc281a/
# "Key 'todo_set-0-id' not found in <QueryDict: .....>"
#

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.