模型.py
class Menu(models.Model):
...
has_submenu=models.BooleanField(default=1)
page=models.ForeignKey(Page,null=True)
我希望 django admin 仅当 has_submenu 复选框为 false 时才显示页面属性(因此 django-admin 必须为我编写一些 javascript :) )
也许我必须扩展 render_change_form
方法
有什么建议吗?
最佳答案
您可以使用jQuery within the Django admin :
class MenuAdmin(admin.ModelAdmin):
# ...
class Media:
js = ('/static/admin/js/hide_attribute.js',)
ModelAdmin
andInlineModelAdmin
have amedia
property that returns a list of Media objects which store paths to the JavaScript files for the forms and/or formsets.
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();
}
})
})
命名空间:
To avoid conflicts with user-supplied scripts or libraries, Django’s jQuery (version 3.3.1) is namespaced as
django.jQuery
.
关于django admin 仅当复选框为 false 时才显示字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15978719/