django的admin后台注册model并显示
在admin后台注册model并显示其他字段:
修改app下的admin.py,注册model:
from .models import vm
admin.site.register(vm) #注册名为vm的model
在admin后台会显示vm内容,但是默认只显示model中return的字段:
比如要显示名为hv的model中的其他字段,修改admin.py如下:
from .models import hv,vm admin.site.register(vm) class hvAdmin(admin.ModelAdmin): #fields = ['sn', 'ip'] fieldsets = [ (None, {'fields': ['name']}), (None, {'fields': ['ip']}), ] list_display = ('name', 'ip') #add more columns. list_filter = ['ip'] #add filter function. search_fields = ['name'] #add search function. admin.site.register(hv,hvAdmin)
结果显示如下: