仅当复选框为false时,django admin才显示字段

时间:2022-09-12 08:52:02

models.py

class Menu(models.Model):

    ...
    has_submenu=models.BooleanField(default=1)
    page=models.ForeignKey(Page,null=True)

I want django admin shows the page attribute only if has_submenu checkbox is false (So django-admin must write some javascript for me :) )

我想django admin只有当has_submenu复选框为false时才显示页面属性(所以django-admin必须为我写一些javascript :))

Maybe i must extend the render_change_form method

也许我必须扩展render_change_form方法

Any advice?

3 个解决方案

#1


8  

My answer:

class MenuAdmin(admin.ModelAdmin):
...
    class Media:
        js = ('/static/admin/js/hide_attribute.js',)

hide_attribute.js

hide_page=false;
django.jQuery(document).ready(function(){
    if (django.jQuery('#id_has_submenu').is(':checked')) {
        django.jQuery(".page").hide();
        hide_page=true;
    }else
    {
        django.jQuery(".page").show();
        hide_page=false;

    }
    django.jQuery("#id_has_submenu").click(function(){
        hide_page=!hide_page;
        if(hide_page)
        {
            django.jQuery(".page").hide();
        }else
        {
            django.jQuery(".page").show();
        }
    })
})

I think this is a simple answer

我认为这是一个简单的答案

#2


1  

How about overriding get_form method on a ModelAdmin, like this:

如何在ModelAdmin上覆盖get_form方法,如下所示:

class MenuModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        self.exclude = []
        if obj and obj.has_submenu:
            self.exclude.append('page')
        return super(MenuModelAdmin, self).get_form(request, obj, **kwargs)

Also, please, see get_form docs.

另外,请参阅get_form docs。

#3


1  

You could extend Django admin template.

你可以扩展Django管理模板。

Just follow this structure:

请遵循以下结构:

Across an entire project:

整个项目:

templates/admin/change_form.html

Across an application

跨应用程序

templates/admin/<my_app>/change_form.html

Across a Model

跨模型

templates/admin/<my_app>/<my_model>/change_form.html

In your case, looks like you only need to extend the Menu model. I would do the following:

在您的情况下,看起来您只需要扩展Menu模型。我会做以下事情:

  1. grab the change_form.html teamplate from django folder
  2. 从django文件夹中获取change_form.html teamplate

  3. inside the object loop, look for the page field
  4. 在对象循环内,查找页面字段

  5. do the condition check on has_submenu to decide whether to show or not the page attribute
  6. 对has_submenu进行条件检查以决定是否显示page属性

#1


8  

My answer:

class MenuAdmin(admin.ModelAdmin):
...
    class Media:
        js = ('/static/admin/js/hide_attribute.js',)

hide_attribute.js

hide_page=false;
django.jQuery(document).ready(function(){
    if (django.jQuery('#id_has_submenu').is(':checked')) {
        django.jQuery(".page").hide();
        hide_page=true;
    }else
    {
        django.jQuery(".page").show();
        hide_page=false;

    }
    django.jQuery("#id_has_submenu").click(function(){
        hide_page=!hide_page;
        if(hide_page)
        {
            django.jQuery(".page").hide();
        }else
        {
            django.jQuery(".page").show();
        }
    })
})

I think this is a simple answer

我认为这是一个简单的答案

#2


1  

How about overriding get_form method on a ModelAdmin, like this:

如何在ModelAdmin上覆盖get_form方法,如下所示:

class MenuModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        self.exclude = []
        if obj and obj.has_submenu:
            self.exclude.append('page')
        return super(MenuModelAdmin, self).get_form(request, obj, **kwargs)

Also, please, see get_form docs.

另外,请参阅get_form docs。

#3


1  

You could extend Django admin template.

你可以扩展Django管理模板。

Just follow this structure:

请遵循以下结构:

Across an entire project:

整个项目:

templates/admin/change_form.html

Across an application

跨应用程序

templates/admin/<my_app>/change_form.html

Across a Model

跨模型

templates/admin/<my_app>/<my_model>/change_form.html

In your case, looks like you only need to extend the Menu model. I would do the following:

在您的情况下,看起来您只需要扩展Menu模型。我会做以下事情:

  1. grab the change_form.html teamplate from django folder
  2. 从django文件夹中获取change_form.html teamplate

  3. inside the object loop, look for the page field
  4. 在对象循环内,查找页面字段

  5. do the condition check on has_submenu to decide whether to show or not the page attribute
  6. 对has_submenu进行条件检查以决定是否显示page属性