使用内联保存的Django Admin ValidationError

时间:2015-09-24 13:01:49

标签: python css django django-forms django-admin

当在模型编辑页面上点击admin中的保存时,我得到了这个回溯:

Traceback (most recent call last):
 File "lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
   response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "lib/python2.7/site-packages/django/contrib/admin/options.py", line 616, in wrapper
   return self.admin_site.admin_view(view)(*args, **kwargs)
 File "lib/python2.7/site-packages/django/utils/decorators.py", line 110, in _wrapped_view
   response = view_func(request, *args, **kwargs)
 File "lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
   response = view_func(request, *args, **kwargs)
 File "lib/python2.7/site-packages/django/contrib/admin/sites.py", line 233, in inner
   return view(request, *args, **kwargs)
 File "lib/python2.7/site-packages/django/contrib/admin/options.py", line 1519, in change_view
   return self.changeform_view(request, object_id, form_url, extra_context)
 File "lib/python2.7/site-packages/django/utils/decorators.py", line 34, in _wrapper
   return bound_func(*args, **kwargs)
 File "lib/python2.7/site-packages/django/utils/decorators.py", line 110, in _wrapped_view
   response = view_func(request, *args, **kwargs)
 File "lib/python2.7/site-packages/django/utils/decorators.py", line 30, in bound_func
   return func.__get__(self, type(self))(*args2, **kwargs2)
 File "lib/python2.7/site-packages/django/utils/decorators.py", line 145, in inner
   return func(*args, **kwargs)
 File "lib/python2.7/site-packages/django/contrib/admin/options.py", line 1466, in changeform_view
   if all_valid(formsets) and form_validated:
 File "lib/python2.7/site-packages/django/forms/formsets.py", line 439, in all_valid
   if not formset.is_valid():
 File "lib/python2.7/site-packages/django/forms/formsets.py", line 304, in is_valid
   self.errors
 File "lib/python2.7/site-packages/django/forms/formsets.py", line 278, in errors
   self.full_clean()
 File "lib/python2.7/site-packages/django/forms/formsets.py", line 325, in full_clean
   for i in range(0, self.total_form_count()):
 File "lib/python2.7/site-packages/django/forms/formsets.py", line 115, in total_form_count
   return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)
 File "lib/python2.7/site-packages/django/forms/formsets.py", line 97, in management_form
   code='missing_management_form',
ValidationError: [u'I dati del ManagementForm sono mancanti oppure sono stati manomessi’]

意大利语中的错误消息为

  

ManagementForm数据丢失或被篡改

我还注意到POST请求包含名称中带有前缀的额外部分以及看起来可疑的空ID:

------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="csrfmiddlewaretoken"

0123456789abcdef
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-TOTAL_FORMS"

41
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-INITIAL_FORMS"

41
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-MIN_NUM_FORMS"

0
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-MAX_NUM_FORMS"

1000
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-0-id"

405718
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-0-property”

87462

[...]

------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-40-id"

462250
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-40-property"

87462
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-__prefix__-id"


------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="model_set-__prefix__-property"

87462
------WebKitFormBoundaryHni3TflxO2xxoMnc
Content-Disposition: form-data; name="_save"

Salva
------WebKitFormBoundaryHni3TflxO2xxoMnc--

Django Inline Admin Formset extra entry has prefix instead of number 建议显示:没有从forms.css(空格式类)中获取,但在我的情况下似乎是。我正在查看Chrome开发者工具。

我的内联定义为:

class ModelInline1(admin.TabularInline):
    model = Model1
    show_change_link = True
    can_delete = False
    extra = 0
    fields = ['created', 'type', 'formatted_text',
              'picture_tag', 'from_seller', 'is_payment']
    readonly_fields = ['created', 'type', 'formatted_text',
                       'picture_tag', 'from_seller', 'is_payment']

class ModelInline2(admin.TabularInline):
    model = Model2
    show_change_link = True
    extra = 0
    fields = ['created', 'place', 'status', 'code']
    readonly_fields = ['created', 'place', 'status', 'code']

@admin.register(models.Conversation)
class ModelAdmin(FSMTransitionMixin, admin.ModelAdmin):
    fsm_field = ['status']
    list_display = ['id', 'buyer', 'item', 'status', 'last_message', 'created']
    list_filter = ['status', 'created']
    fields = ['status', 'created', 'last_message', 'tracking_number']
    readonly_fields = ['status', 'created', 'last_message', 'tracking_number']
    inlines = [ModelInline1, ModelInline2]
    ordering = ['-created']
    search_fields = [
        'id',
        'buyer__username',
        'item__name',
        'item__user__username'
    ]

1 个答案:

答案 0 :(得分:1)

错误基本上是Django admin: missing inlines for some records。 我没注意到第二个内联完全丢失了。当内联中使用的模型的__unicode__方法引发错误时,会发生这种情况。不幸的是,在这种情况下,管理员无需呈现内联而无声地失败,但是保存表单失败了ValidationError