ValidationError [u'ManagementForm数据丢失或仅在特殊情况下被篡改]

时间:2017-05-11 18:29:03

标签: python django django-admin

通过Django Admin编辑模型字段时,我收到[u'ManagementForm data is missing or has been tampered with']验证错误。

步骤:

  • 通过Django Admin编辑模型并插入一个特殊字符 (é,ñ)
  • 数据保存好。
  • 再次编辑模型字段时 (charField)无论输入是什么,都会引发验证错误。

在没有特殊字符的情况下进行编辑时,表格工作正常。

修改

保存特殊字符时,该模型的内联不会显示在编辑部分中,因此在这种情况下验证错误是正确的。

相关代码:

class StreamInline(admin.TabularInline):
    model = Stream
    form = StreamForm
    extra = 0
    #define order
    fields = ('name', 'canal', 'tipo', 'stream_type',
              'unit', 'formula', 'label', 'color',
              ('min_scale', 'max_scale', 'fixed_scale'), 'enabled')
    readonly_fields = ['name', 'stream_type']
    can_delete = False

    class Media:
        js = ('js/jquery-1.7.2.min.js', 'js/jscolor.min.js',)


class NodeAdmin(admin.ModelAdmin):
    search_fields = ['name', ]

    fields = (('identifier', 'name', 'node_type'), ('group','cultivation') , ('longitude', 'latitude','operator','sim_card','telephone'), 'config', ('enabled', 'deleted'),('date_node','date_bat','reference_irrig_date'),'notes')
    list_display = ['identifier', 'name', 'group', 'sim_card']
    list_filter = ('group__name',)

    #form = NodeForm

    #list_filter = ['deleted', 'enabled', 'node_type']
    inlines = [StreamInline]

Django版本:1.4.21

Python版本:2.7.9

1 个答案:

答案 0 :(得分:1)

错误:[u'ManagementForm data is missing or has been tampered with']是因为内联没有出现,所以MAX_TOTAL_FORMS和其他内容并不一致。

由于模型中的__unicode__函数存在错误,因此未显示内联。

__unicode__函数中返回unicode类型(python 2)解决了这个问题。