Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiValueDictKeyError in Django admin

UPDATE

model:

class PicturesManager(models.Manager):
    def create_pictures(self, flat, img):
        pictures = self.create(car=car, image=img)
        return pictures

def get_file_path(instance, filename):
    filename = "%s.%s" % (uuid.uuid4(), filename.split('.')[-1])
    return os.path.join('car_img/', filename)

class Pictures(models.Model):
    car = models.ForeignKey(Car, verbose_name="Car Image", related_name='pictures')
    image = models.ImageField(verbose_name="Photo", upload_to = get_file_path, max_length=64)
    objects = PicturesManager()
    def __unicode__(self):
        return str(self.id)

admin:

class MyInlineModelAdmin(admin.TabularInline):
    model = Pictures
    extra = 3
class CarOptionsInline(admin.StackedInline):
    model = CarOptions

class WatchesInline(admin.StackedInline):
    model = Watches

class CarAdmin(admin.ModelAdmin):
    inlines = [CarOptionsInline, WatchesInline, MyInlineModelAdmin]

When I edit Car in admin, I have error: MultiValueDictKeyError at ... "u'pictures-0-id'"

Traceback:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin1/cars/car/1/

Django Version: 1.6c1
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sitemaps',
 'cars',
 'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
  430.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
  339.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in change_view
  1227.             if all_valid(formsets) and form_validated:
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in all_valid
  415.         if not formset.is_valid():
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in is_valid
  292.         err = self.errors
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in errors
  267.             self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in full_clean
  314.             form = self.forms[i]
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __get__
  49.         res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in forms
  133.         forms = [self._construct_form(i) for i in xrange(self.total_form_count())]
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _construct_form
  839.         form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _construct_form
  555.             pk = self.data[pk_key]
File "/usr/local/lib/python2.7/dist-packages/django/utils/datastructures.py" in __getitem__
  301.             raise MultiValueDictKeyError(repr(key))

Exception Type: MultiValueDictKeyError at /admin1/cars/car/1/
Exception Value: "u'pictures-0-id'"

How to fix it? I use Django 1.6c1.

Thanks!

like image 365
Lev Avatar asked Nov 02 '13 11:11

Lev


People also ask

What is MultiValueDictKeyError?

Most of the case's this MultiValueDictKeyError occurred for missing key in the dictionary-like request object. Because dictionary is an unordered key, value pair “associative memories” or “associative arrays” In another word. request. GET or request.

Can I use Django admin as frontend?

Django Admin's task is to provide an interface to the admin of the web project. Django's Docs clearly state that Django Admin is not made for frontend work.

What is Is_private in Django?

The line is is_private = request.POST['is_private'] The problems lies within the form, the is_private is represented by a checkbox. If the check box is NOT selected, obviously nothing is passed. This is where the error gets chucked.

How can I remove extra's from Django admin panel?

Answer 1: You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories .


1 Answers

I think it is related to a Django 1.6 ticket

#ticket 13696 -- ensured inline pk field is rendered

You have to update the following admin templates:

  • stacked.html
  • tabular.html

Remove the single line:

{% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}

and add the lines:

{% if inline_admin_form.needs_explicit_pk_field %}
    {{ inline_admin_form.pk_field.field }}
{% endif %}
like image 158
RMotitsuki Avatar answered Oct 04 '22 11:10

RMotitsuki