简体   繁体   English

Django中的if语句无法正常工作

[英]If statement in Django not working in properly

I have a small project and I have been unable to get the following statement to work. 我有一个小项目,但无法执行以下语句。 Any help would be great. 任何帮助都会很棒。 User inputs can either self.sale_head which is a $ value,if a $ value is not add a self.estimated_weight_hd is used to get the total weight, the code below should return a estimated_weight_total which can be used for the total sale price. 用户输入可以self.sale_head这是一个$值,如果$值不加self.estimated_weight_hd用于获取的总重量,下面的代码应返回estimated_weight_total可用于总销售价格。 All works when I add the estimated_weight_total manually. 当我手动添加estimated_weight_total时,所有方法均有效。 I am lost as why. 我迷失了为什么。

def calc_estimated_weight_total(self):
    if self.sale_head <= 0:
        amount = (self.number * self.estimated_weight_hd)
        return amount

def save(self):
    self.estimated_total_weight = self.calc_estimated_weight_total()
    super(SaleNote, self).save()

Your code doesn't do what you want cause if self.sale_head > 0 nothing will return. 如果self.sale_head > 0则您的代码无法执行您想要的操作,则不会返回任何内容。 From your description, I think your code should be somthing like: 从您的描述来看,我认为您的代码应该是这样的:

def calc_estimated_weight_total(self):
    if self.sale_head <= 0:
        amount = (self.number * self.estimated_weight_hd)
        return amount
    else:
        return something_only_you_know

def save(self):
    self.estimated_total_weight = self.calc_estimated_weight_total()
    super(SaleNote, self).save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM