从Django管理员中减去两个字段 [英] subtract two fields from django admin
本文介绍了从Django管理员中减去两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从django admin中减去两个字段值.在这里,我发表了自己的看法和圣旨
How do i subtract the two field values from django admin. here i gave my views and templtaes
views.py
for attendancesheet_info in attendance_sheet:
attendancesheet_info.present_days= attendancesheet_info.working_days - attendancesheet_info.leave_days
模板
</tr>
<tr>
{% for attendancesheet_info in attendance_sheet %}
<tr>
<td>{{ attendancesheet_info.id}}</td>
<td>{{ attendancesheet_info.employeeid}}</td>
<td>{{ attendancesheet_info.employeename}}</td>
<td>{{ attendancesheet_info.working_days}}</td>
<td>{{ attendancesheet_info.leave_days}}</td>
<td>{{ attendancesheet_info.permissions}}</td>
<td>{{ attendancesheet_info.present_days}}</td>
</tr>
{% endfor %}
</tr>
推荐答案
您可以在模型的类方法中对其进行计算,并在admin的编辑视图或列表视图中以只读字段的形式在admin中引用该方法..py.
You can calculate it in a class method in the model and reference that method in the admin as a read-only field in an edit view or list view in admin.py.
# this method goes into your model.
@property
def present_days(self):
return self.working_days - self.leave_days
您的示例模板也将起作用.您无需在视图中执行任何操作即可访问 present_days
.不需要来自views.py的问题代码.
Your example template will also work. You don't need to do anything in the view to access present_days
. The code in your question from views.py is not needed.
<td>{{ attendancesheet_info.present_days}}</td>
这篇关于从Django管理员中减去两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文