```
class History(BaseModel):
Sn = models.ForeignKey('Jig', verbose_name='编码', on_delete=models.CASCADE)
Rule = models.ForeignKey('Rule', verbose_name='规则',on_delete=models.CASCADE)
Result_choice = ((None, '请选择 '), (0, '已保养'), (1, '未保养'))
Result = models.IntegerField(default=1, choices=Result_choice, verbose_name='结果')
MaintainTime = models.DateTimeField(verbose_name='时间')
class JigLedger(BaseModel):
Sn = models.ForeignKey('Jig', verbose_name='编码', on_delete=models.CASCADE)
Choice = (
(None, '请选择'), (0, '正常使用'), (1, '保养预警'), (2, '保养过期'), (3, '维修中'), (4, '报废'), (5, '封存'))
Status = models.IntegerField(default=0, choices=Choice, verbose_name='状态')
Manager = models.ForeignKey(User, verbose_name='管理', on_delete=models.CASCADE)
creator = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='creator', on_delete=models.PROTECT,
verbose_name='操作人员')
history = HistoricalRecords()
class HistoryAdmin(admin.ModelAdmin):
list_display = ('Sn', 'status', 'Manager', 'creator', 'default_status', 'create_time', 'last_update_time',)
def get_queryset(self, request, *args, **kwargs):
return JigLedger.objects.all()
def status(self, obj):
if obj.Status == 1:
return u'%s' % '保养预警'
elif obj.Status == 0:
return u'%s' % '正常使用'
elif obj.Status == 2:
return u'%s' % '保养过期'
elif obj.Status == 3:
return u'%s' % '维修中'
elif obj.Status == 4:
return u'%s' % '报废'
else:
return u'%s' % '封存'
status.short_description = '状态'
def Manager(self, obj):
return obj.Manage
Manager.short_description = '管理'
def creator(self, obj):
return obj.creator
creator.short_description = '操作人员'
def create_time(self, obj):
return obj.create_time
creator.short_description = '创建时间'
def last_update_time(self, obj):
return obj.last_update_time
def has_add_permission(self, request):
# 禁用添加按钮
return False
def has_delete_permission(self, request, obj=None):
# 禁用删除按钮
return False
# 操作的是JigLedger
def default_status(self, obj):
if obj.Status == 1 or obj.Status == 2:
return format_html(
'<span style="color: red;">{}</span>',
'NG'
)
else:
return format_html(
'<span style="color: green;">{}</span>',
'OK'
)
default_status.short_description = '维保状态'
def save_model(self, request, obj, form, change):
Sn = request.POST.get('Sn')
Rule = request.POST.get('Rule')
Result = request.POST.get('Result')
MaintainTime = request.POST.get('MaintainTime')
```
想实现的功能是
History.objects.create(Sn=Sn,Rule=Rule,Result=Result,MaintainTime=MaintainTime)
```
```
目前的obj 是JigLedger,而不是History
```
super(HistoryAdmin, self).save_model(request, obj, form, change)
admin.site.register(History, HistoryAdmin)
thanks!