我想知道是否可以在ListView的每個記錄上應用規則例如:從模型中獲取以克爲單位的值並創建包含kg值的新var,如果克值> 1000Django:操縱generic.ListView中的數據
我試圖用的get_object但它僅適用於ViewDetail
在此先感謝您的幫助:)
class ViewBsin(generic.ListView):
template_name = 'browser/bsin.html'
context_object_name = 'gtin_list'
model = Gtin
...
def get_object(self):
# Call the superclass
object = super(ViewBsin, self).get_object()
# Add new data
if object.M_G and object.M_G >= 1000:
object.M_KG = object.M_G/1000
if object.M_ML and object.M_ML >= 1000:
object.M_L = object.M_ML/1000
object.save()
# Return the object
return object
thx!但我必須存儲這些數據,因爲單位是克拉(克),我想顯示爲KiloGram(Kg)..因爲1公斤= 1000克;)...做到這一點的唯一方法是分成模板??但沒有辦法將模板分成:(:(你的答案:)( – Philippos