简体   繁体   English

调试Django管理面板错误

[英]Debugging Django admin panel bug

When I navigate to the admin interface for a particular model (the whole-table view), and simply hit 'Save', this error was popping up on the usual red banner: 当我导航到特定模型的管理界面(全表视图),然后简单地单击“保存”时,此错误在通常的红色横幅上弹出:

Please correct the errors below.

Needless to say I couldn't actually make edits from this view, until I experimented and fixed it. 不用说,在我尝试并修复它之前,我实际上无法从该视图进行编辑。

Here's my class: 这是我的课:

 class RoleMapping(models.Model):
     MIN_LENGTH, MAX_LENGTH = 3, 40
     role_name = models.CharField(unique=True, max_length=MAX_LENGTH, validators = [
         MinLengthValidator(MIN_LENGTH, "Field length should be greater than {}".format(MIN_LENGTH))
     ])
     role_type = models.ForeignKey(RoleType, null=True, blank=True )

Here's the admin interface model. 这是管理界面模型。 But, flipping around some of the editable fields seems to have made things work. 但是,翻转一些可编辑字段似乎可以使事情工作。

 class RoleMapping(admin.ModelAdmin):
     model = RoleMapping
     list_display = ('role_name', 'role_type',)
     #list_editable = ('role_name', 'role_type',) # This fails
     #list_editable = ('role_name',) # This fails
     list_editable = ('role_type',) # This works?!

I can get it working, pretty easily, by keeping role_type as the only editable type. 通过将role_type保留为唯一可编辑的类型,我可以轻松地使其工作。 However, I only found this out after a bit of trial and error. 但是,经过反复试验,我才发现这一点。 I'm wondering: 我很好奇:

  1. What's the django way of debugging these sorts of admin-panel-ORM issues in the future 将来调试这些类型的admin-panel-ORM问题的django方法是什么

  2. Why might it have been failing in the first place? 为什么它首先会失败?

According to the Django admin docs : 根据Django管理文档

Note: list_editable interacts with a couple of other options in particular ways; 注意: list_editable以特定的方式与其他几个选项交互。 you should note the following rules: 您应注意以下规则:

  • Any field in list_editable must also be in list_display . list_editable任何字段也必须在list_display You can't edit a field that's not displayed! 您无法编辑未显示的字段!
  • The same field can't be listed in both list_editable and list_display_links – a field can't be both a form and a link. 不能在list_editablelist_display_links列出相同的字段-一个字段不能既是表单又是链接。

You'll get a validation error if either of these rules are broken. 如果这些规则中的任何一条被破坏,您将收到验证错误。

My guess is that you are violating the second point and that role_name is by default the sole member of list_display_links . 我的猜测是您违反了第二点,默认情况下, role_namelist_display_links的唯一成员。

To test this, set list_display_links = None or make another field and add it to list_display and list_display_links . 要对此进行测试,请设置list_display_links = None或创建另一个字段并将其添加到list_displaylist_display_links

Edit: 编辑:

I suspect that the following will give you what you want. 我怀疑以下内容会给您您想要的。 I've simply added id as the first element of list_display . 我只是将id添加为list_display的第一个元素。 Behind the scenes, this adds id instead of role_name to list_display_links , which frees up role_name to be allowed in list_editable . 在幕后,这增加了id ,而不是role_namelist_display_links ,从而释放role_name在被允许list_editable

class RoleMapping(admin.ModelAdmin):
     model = RoleMapping
     list_display = ('id', 'role_name', 'role_type',)
     list_editable = ('role_name', 'role_type',)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM