8

更新

模型:

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)

行政:

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]

当我在管理员中编辑 Car 时,出现错误:MultiValueDictKeyError at ... "u'pictures-0-id'"

追溯:

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'"

如何解决?我使用 Django 1.6c1。

谢谢!

4

6 回答 6

19

我认为这与 Django 1.6 票证有关

#ticket 13696 -- 确保呈现内联 pk 字段

您必须更新以下管理模板:

  • 堆叠的.html
  • 表格.html

删除单行:

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

并添加以下行:

{% if inline_admin_form.needs_explicit_pk_field %}
    {{ inline_admin_form.pk_field.field }}
{% endif %}
于 2013-11-27T15:18:09.303 回答
13

如果您已经在使用较新版本的 django 并且仍然得到它,请确保您的非 AutoField PK 在模型定义中具有“editable=False”。

于 2015-12-02T20:05:33.213 回答
2

对于 Django <= 1.7.3,还要确保内联模型的主键字段是 AutoField:

错误的:

class Car(models.Model):
    id = IntegerField(primary_key=True)

正确的:

class Car(models.Model):
    id = AutoField(primary_key=True)

https://code.djangoproject.com/ticket/15665

于 2015-01-24T11:17:36.497 回答
1

为我解决的问题是将 grappelli 更新到 2.5.1(假设您使用的是 grappelli ofc)。我认为这是因为它执行指定的管理模板更新。

pip install django-grappelli==2.5.1
于 2014-01-14T16:15:28.917 回答
0

答:更新管理模板。

于 2013-11-05T20:52:43.470 回答
0

如果您使用的是 django-suit。您确实需要将版本更新django-suit到与您的 django 版本兼容的最新版本。就我而言,我使用django 1.10并更新了0.2.25 -> 0.2.28.

pip install django-suit==0.2.28

于 2019-11-13T20:27:35.663 回答