应该使用attribute,object是model类的实例,而不是dict
var_a = obj.foo
var_b = obj.bar
result = var_a * var_b
最好是在模型的声明中创建属性:
Class MyModel(models.Model):
foo = models.FloatField()
bar = models.FloatField()
@property
def multiply_fb(self):
return self.foo * self.bar
在他们看来:
class MyView(generic.ListView):
template_name = "my_item_list.html"
model = MyModel
def get_context_data(self, **kwargs):
obj = MyModel.objects.get(pk=this_object_id)
context = {
'result': obj.multiply_fb,
}
return context