【问题标题】:i have added custom field to list_display but when add that field list_editable in django admin i get error:adminE121我已将自定义字段添加到 list_display 但是在 django admin 中添加该字段 list_editable 时出现错误:adminE121
【发布时间】:2020-03-04 12:04:45
【问题描述】:

我在模型管理类上有一个方法,它可以在 list_display 上获取反向关系字段的数据,但是当我在 list_editable 中添加该字段时,我得到一个错误

<class 'app.admin.CustomerAdmin'>: (admin.E121) The value of 'list_editable[2]' refers to 'box_status', which is not an attribute of 'app.Customer'.

这是模型管理类

@admin.register(Customer)
class CustomerAdmin(AbstractModelAdmin):
    class Media:

    list_display = ['name', 'email', 'phone', 'stylist', 'box_schedule', 'drop_off_step', 
                    'box_status', 'age''created_at', 'payment_status']
    list_display_links = ('name', 'email')
    search_fields = ['name', 'email', 'id']
    list_editable = ['stylist', 'payment_status', 'box_status']

下面是我从相关模型字段获取的 box_status 方法。

    def box_status(self, obj):
        det = list(obj.box.values_list('box_status', flat=True))
        return det

这在 list_display 中有效,但在 list_editable 中出现系统错误。

【问题讨论】:

    标签: django django-admin


    【解决方案1】:

    一方面,您正在使用可编辑列表中的方法。 Django 只允许在 list_editable 中编辑模型字段。不是方法的“返回”。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 我知道亲爱的,但我想覆盖默认行为,所以请提供任何帮助
      • 你能解释一下你为什么需要这个吗?
      • 我需要这个,因为我的相关模型字段之一正在列表显示中工作,但我想要的是用户可以通过使其可编辑来直接更改此页面上的相关模型字段。
      • 我不确定,我很理解你,但也许它对你有帮助docs.djangoproject.com/en/3.0/ref/contrib/admin/…
      • 谢谢!但我得出的结果是我们无法在 django list_editables 中添加模型字段以外的字段。
      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2013-12-30
      • 2020-01-10
      • 2018-03-05
      • 1970-01-01
      相关资源
      最近更新 更多