2014-12-19 95 views
0

如何從django admin中減去兩個字段值。在這裏,我給我的意見和templtaes從django admin減去兩個字段

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> 

回答

0

假設你有你的models.py定義如下AttendanceSheet的模型,你可以創建一個ModelAdminadmin.py中爲此型號設置,並指定要在管理頁面上顯示的字段。

請參閱the official documentation與此主題相關,尤其是exclude option

1

您可以在模型中的類方法中計算它,並在admin中將該方法引用爲admin.py中的編輯視圖或列表視圖中的只讀字段。

# this method goes into your model. 
@property 
def present_days(self): 
    return self.working_days - self.leave_days 

您的示例模板也可以使用。您無需在視圖中執行任何操作即可訪問present_days。 view.py中問題的代碼是不需要的。

<td>{{ attendancesheet_info.present_days}}</td> 
+0

它不calaculating ..just它打印0 .. – Geetha 2014-12-20 04:39:50

+0

如果self.working_days == self.leave_days,它將返回0。 那你在這種情況下,期望? – 2014-12-20 13:35:34

+0

你的模特看起來像什麼。發佈源代碼。 – 2014-12-20 13:37:46