Django条件管理员list_editable [英] Django conditional admin list_editable

查看:647
本文介绍了Django条件管理员list_editable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有在每个对象的基础上将list_editable设置为可选的?例如,只读字段属性具有此选项,该选项不会影响changelist_view。

Is there anyway to make the list_editable optional on a per object bases? For example the readonly fields attribute has this option, which doesn't affect the changelist_view.

class MyAdmin(admin.ModelAdmin):
    readonly_fields = ('foo',)

    def get_readonly_fields(self, request, obj=None):
        fields = super(MyAdmin, self).get_readonly_fields(request, obj=obj)

        if obj.status == 'CLOSED':
            return fields + ('bar',)
        return fields

list_display和其他一些属性也可以实现相同的效果。似乎没有方法 get_list_editable_fields。

The same can be achieved for list_display and some other attributes. It seems there isn't a method 'get_list_editable_fields'.

我希望某些行显然是不可变的,但是除了引发粗俗的错误之外,这似乎行不通。我也没有找到关于该属性的任何文档

I want some of the rows to be immutable obviously, but other than raising a vulgar error doesn't seem to work. I didn't find any documentation about the attribute either

是否可以通过list_display getter渲染小部件?

Would it somehow be possible to render the widget via a list_display getter?

class MyAdmin(admin.ModelAdmin):
    list_display = ('get_bar',)
    list_editable = ('get_bar',)

    def get_bar(self, obj):
        return widget or str(obj.bar)  # ???
    get_bar.allow_tags = True

使用Alasdair的反馈进行更新:

update using Alasdair's feedback:

def get_changelist_formset(self, request, **kwargs):
    """
    Returns a FormSet class for use on the changelist page if list_editable
    is used.
    """
    # I run through this code for each row in the changelist, but there's nothing in kwargs, so I don't know how to use the instance as a guide to which fields should be in list_editable?

    defaults = {
        "formfield_callback": partial(self.formfield_for_dbfield, request=request),
    }
    defaults.update(kwargs)
    return modelformset_factory(
        self.model, self.get_changelist_form(request), extra=0,
        fields=self.list_editable, **defaults
    )


推荐答案

如您所说,没有 get_list_editable 方法

尝试覆盖 get_changelist_formset 方法。我认为您需要复制整个方法,然后将传递给 modelformset_factory 的字段列表更改。

Try overriding the get_changelist_formset method. I think you'll need to duplicate the entire method, and change the list of fields passed to modelformset_factory.

这篇关于Django条件管理员list_editable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆