如何在Django管理中崩溃一个字段? [英] How To Collapse Just One Field in Django Admin?

查看:219
本文介绍了如何在Django管理中崩溃一个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

django管理员可以让您指定字段集。正确构造一个将不同字段组合在一起的元组。您还可以为某些字段组指定类。其中一个类是崩溃,这将隐藏在可折叠区域下的字段。这对于隐藏很少使用或高级字段来保持UI清洁是有好处的。

The django admin allows you to specify fieldsets. You properly structure a tuple that groups different fields together. You can also specify classes for certain groups of fields. One of those classes is collapse, which will hide the field under a collapsable area. This is good for hiding rarely used or advanced fields to keep the UI clean.

然而,我有一种情况,我想在许多不同的应用程序中隐藏一个寂寞的字段。这将是大量的打字,以便在每个admin.py文件中创建一个完整的字段集规范,只是将一个字段放入折叠区域。这也造成了一个困难的维护情况,因为我每次编辑相关联的模型时都必须编辑字段集。

However, I have a situation where I want to hide just one lonesome field on many different apps. This will be a lot of typing to create a full fieldset specification in every admin.py file just to put one field into the collapsed area. It also creates a difficult maintenance situation because I will have to edit the fieldset every time I edit the associated model.

我可以很容易地使用排除选项。我想要一些类似的崩溃。这是可能的吗?

I can easily exclude the field entirely using the exclude option. I want something similar for collapse. Is this possible?

推荐答案

Django没有内置的方式做这个我知道,但我可以想想几种方法你可以做一些事情,而不是手动修改很多的字段集。

Django doesn't have a built in way of doing this that I'm aware of but I can think of a couple of ways you could do something once, rather than having to manually modify lots of fieldsets.

一种方法是使用javascript来重写页面标记。也许javascript可以有一个字段名称列表,只要它找到其中一个隐藏字段,它的标签,并添加一个按钮到页面切换这些不可见字段。

One approach would be to use javascript to rewrite the page markup. Maybe the javascript could have a list of fieldnames and whenever it finds one of those it hides the field and it's label and adds a button to the page to toggle these invisible fields.

另一种方法只涉及python。通常,您只需将adminets中的fieldsets属性指定为元组。但是您可以将其指定为导入函数,它将常用的元组作为参数。在您的设置文件中,您可以指定要隐藏的字段名称列表。然后,您需要编写一个返回修改的元组的函数,将与您的一个字段名称匹配的任何字段与折叠类一起移动到新的字段集中。

The other approach would just involve python. Normally you just specify the fieldsets attribute in the admin as a tuple. But you could specify it as an imported function which takes the usual tuple as an argument. In your settings file you could specify a list of fieldnames you want to hide. You then need to write a function that returns a modified tuple, moving any fields that match one of your fieldnames into a new fieldset along with the collapse class.

例如您的管理员类可以执行此操作(您需要编写和导入hide_fields)。

For example in your admin class you could do something like this (you need to write and import hide_fields).

fieldsets = hide_fields(
    (None,
        {'fields':('title', 'content')}
    )
)

这可能会被解释为以下内容,假设内容在设置文件中是要隐藏的东西:

This might end up being interpreted as the following, assuming content is in the settings file as something you want to hide:

fieldsets = (
    (None,
        {'fields':('title',)}
    ),
    ('Extra',
        {
            'fields':  ('content',),
            'classes':('collapse',),
        }
    ),
)

这篇关于如何在Django管理中崩溃一个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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