django admin 根据choice字段选择的不同来显示不同的页面

一、举例

tip/tip.js
var react = function () {
if (django.jQuery('#id_tiptype').val() == 'content') {
django.jQuery('#id_content').parent().parent().show(500);
django.jQuery('#id_image').parent().parent().hide(500);
django.jQuery('#id_cropping').parent().parent().hide(500);
}
else {
django.jQuery('#id_content').parent().parent().hide(500);
django.jQuery('#id_image').parent().parent().show(500);
django.jQuery('#id_cropping').parent().parent().show(500);
}
}; #当选择的类型改变的时候触发react函数
django.jQuery(function () {
react();
django.jQuery('#id_tiptype').on('change', react);
});

二、admin.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals from django.contrib import admin
from image_cropping import ImageCroppingMixin from salmonella.admin import SalmonellaMixin from tip.models import Tip,TipTag @admin.register(Tip)
class TipAdmin(ImageCroppingMixin, SalmonellaMixin, admin.ModelAdmin):
# search_fields = ('tiptype',)
list_filter = ('enabled',)
list_display = ('tiptype', 'enabled', 'get_tag','image')
salmonella_fields = ('tags', ) # fields = ('tiptype', 'enabled','tags','image') def get_tag(self, obj):
print(obj.tags.all())
if obj.tags.all():
tag_list = [i.name for i in obj.tags.all()]
return ','.join(tag_list)
else:
return '' get_tag.short_description = '小贴士标签' class Media():
js = ('tip/tip.js',) @admin.register(TipTag)
class TiptagAdmin(admin.ModelAdmin):
search_fields = ('name', )
list_display = ("name", )
上一篇:如何优化TableView


下一篇:ElasticSearch实战系列一: ElasticSearch集群+Kinaba安装教程