自定义记录返回值班 和 表名
class Guys(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField() def __str__(self): # 记录返回值pyton 2用__unicode__ return u'%s %s' % (self.first_name, self.last_name) class Meta: # 定义表名 verbose_name = '人员管理' verbose_name_plural = '人员管理'
关联字段做字 search字段
models.py: class ApplyTag(models.Model): tag = models.CharField('应用实例名', max_length=64) note = models.CharField('备注', max_length=64, null=True, blank=True) def __str__(self): return self.tag class RealServer(models.Model): """ WEB源站 """ apply_name = models.ForeignKey('ApplyTag', verbose_name='应用名', null=True, blank=True) host = models.ForeignKey('Host',verbose_name='主机') bindip_host_eth = models.CharField('主机服务绑定IP的网卡', max_length=2, choices=bindip_host_eth_choices, default='00') port = models.IntegerField('服务端口', null=True, blank=True)
admin.py: class RealServerAdmin(admin.ModelAdmin): list_display = ('apply_name', 'host', 'port') search_fields = ('apply_name__tag',) # 以__ 表示关联字段属性 raw_id_fields = ('host',) admin.site.register(models.RealServer, RealServerAdmin)
list_display = ('first_name', 'last_name', 'email') #添加字段,以列表显示,not a ManyToManyField
search_fields = ('first_name', 'last_name') #页面添加查询栏
list_filter = ('publication_date',) #添加过滤(这里是过滤日期)
exclude = ('xxx',) # 排除字段
date_hierarchy = 'publication_date' #过滤(日期的另外一种过滤方式,可以添加后看一下)
ordering = ('-publication_date',) #排序(这里以日期排序,加‘-’表示降序)
fields = ('title', 'authors', 'publisher') #排除一些不想被其他人编辑的fields(不包含在内的不能编辑,这里publication_date不能编辑了)
filter_horizontal = ('authors',) #从‘多选框’的形式改变为‘过滤器’的方式,水平排列过滤器,must be a ManyToManyField,且不能用于 ForeignKey字段,默认地,管理工具使用`` 下拉框`` 来展现`` 外键`` 字段
filter_vertical = ('authors',) #同上,垂直排列过滤器
raw_id_fields = ('publisher',) #将ForeignKey字段从‘下拉框’改变为‘文本框’显示
自定义字段显示(自定义列)
from django.contrib import admin class PayAccount(models.Model): customer = models.OneToOneField(Customer, verbose_name=u'客户') balance = models.FloatField(verbose_name=u'余额(元)', default=0) remark = models.CharField(max_length=200, verbose_name='备注', null=True, blank=True) def __unicode__(self): return self.customer.name class Meta: verbose_name = u'预缴账户' verbose_name_plural = u'预缴账户' #admin.py class PayAccountModelAdmin(CustomizeModelAdmin): def get_account_state(self, obj): if obj.balance < 0: return u'<span style="color:red;font-weight:bold">%s</span>' % (u"已欠费",) elif obj.balance <= 50: return u'<span style="color:orange;font-weight:bold">%s</span>' % (u"余额不足",) else: return u'<span style="color:green;font-weight:bold">%s</span>' % (u"正常",) get_account_state.short_description = u'账户状态' get_account_state.allow_tags = True list_display = ['customer', 'balance', 'get_account_state', 'remark']
效果: