class Money(admin.ModelAdmin): list_display = ('user','get_idcad','chuqin','qingjia','chizao','yingde','shide','remark',) list_filter = ['user','chuqin','qingjia','chizao','yingde','shide',] # 外键关联中,如何选择一种属性后展示另外一种属性 def get_idcad(self, obj): return obj.gongzi.idcad # 这是我们要展示的字段
get_idcad.short_description = '工资卡号' get_idcad.admin_order_field = 'gongzi__idcad' admin.site.register(models.Money, Money)
数据库设计: 定义外键关联
class GgongZiID(models.Model):
user = models.ForeignKey(to="UserInfo", verbose_name="所属员工", on_delete=models.CASCADE)
idcad = models.CharField(verbose_name="工资卡号",max_length=55)
def __str__(self):
return self.user.name
class Meta:
verbose_name = "工资卡列表"
verbose_name_plural = verbose_name
db_table = 'GgongZiID'
class Money(models.Model):
user = models.ForeignKey(to="UserInfo", verbose_name="员工姓名",on_delete=models.CASCADE)
gongzi = models.ForeignKey(to="GgongZiID", verbose_name="工资卡号",on_delete=models.CASCADE)
chuqin = models.IntegerField(verbose_name="出勤天数")
qingjia = models.IntegerField(verbose_name="请假天数")
chizao = models.IntegerField(verbose_name="迟到早退总数")
yingde = models.FloatField(verbose_name="应得工资")
shide = models.FloatField(verbose_name="实得工资")
remark = models.TextField(max_length=255,verbose_name="备注")
def __str__(self):
return self.user.name
class Meta:
verbose_name = "工资条"
verbose_name_plural = verbose_name
db_table = 'Money'
目的:要通过选择工资员工的姓名,保存后展示选择工资姓名的工资卡号