我想知道是否可以在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