django admin
from django.contrib import admin # Register your models here. from Sansa import models class ServerInline(admin.TabularInline): model = models.Server exclude = ('memo',) #不包含的字段 #readonly_fields = ['create_date'] class CPUInline(admin.TabularInline): model = models.CPU exclude = ('memo',) readonly_fields = ['create_date'] #指定的字段 class NICInline(admin.TabularInline): model = models.NIC exclude = ('memo',) readonly_fields = ['create_date'] class RAMInline(admin.TabularInline): model = models.RAM exclude = ('memo',) readonly_fields = ['create_date'] class DiskInline(admin.TabularInline): model = models.Disk exclude = ('memo',) readonly_fields = ['create_date'] class AssetAdmin(admin.ModelAdmin): list_display = ('id','asset_type','sn','name','manufactory','management_ip','idc','business_unit','admin','trade_date','status') inlines = [ServerInline,CPUInline,RAMInline,DiskInline,NICInline] #内联的表的列表,可以将包含的表显示在同一个编辑页面 search_fields = ['sn',] list_filter = ['idc','manufactory','business_unit','asset_type'] choice_fields = ('asset_type','status') fk_fields = ('manufactory','idc','business_unit','admin') list_per_page = 10 list_filter = ('asset_type','status','manufactory','idc','business_unit','admin','trade_date') dynamic_fk = 'asset_type' dynamic_list_display = ('model','sub_asset_type','os_type','os_distribution') dynamic_choice_fields = ('sub_asset_type',) m2m_fields = ('tags',) class NicAdmin(admin.ModelAdmin): list_display = ('name','macaddress','ipaddress','netmask','bonding') search_fields = ('macaddress','ipaddress') class EventLogAdmin(admin.ModelAdmin): list_display = ('name','colored_event_type','asset','component','detail','date','user') search_fields = ('asset',) list_filter = ('event_type','component','date','user') from django.contrib.contenttypes.models import ContentType from django.http import HttpResponseRedirect class NewAssetApprovalZoneAdmin(admin.ModelAdmin): list_display = ('sn','asset_type','manufactory','model','cpu_model','cpu_count','cpu_core_count','ram_size','os_distribution','os_release','date','approved','approved_by','approved_date') actions = ['approve_selected_objects'] #自定义批量执行的操作 def approve_selected_objects(modeladmin, request, queryset): print(modeladmin,request,queryset) selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME) ct = ContentType.objects.get_for_model(queryset.model) #ContenType实现了对所有的models的封装,实现动态的调用models return HttpResponseRedirect("/asset/new_assets/approval/?ct=%s&ids=%s" % (ct.pk, ",".join(selected))) #pk为Centent表的id,也就是主键,代表调用了哪个表 approve_selected_objects.short_description = "批准入库" # admin.site.unregister(Group) admin.site.register(models.Asset,AssetAdmin) admin.site.register(models.Server) admin.site.register(models.NetworkDevice) admin.site.register(models.IDC) admin.site.register(models.BusinessUnit) admin.site.register(models.Contract) admin.site.register(models.CPU) admin.site.register(models.Disk) admin.site.register(models.NIC,NicAdmin) admin.site.register(models.RAM) admin.site.register(models.Manufactory) admin.site.register(models.Tag) admin.site.register(models.Software) admin.site.register(models.EventLog,EventLogAdmin) admin.site.register(models.NewAssetApprovalZone,NewAssetApprovalZoneAdmin)
ContentType是对app和其中的表做了进步一抽象,可以实现对于model的动态调用;
ContentTpye表的内容: