创建一个简单的python计算以在管理面板中显示特定对象记录

时间:2019-06-27 04:54:19

标签: python django

我对python还是很陌生,并且正在研究一个新的django项目,并希望使用记录数据在管理面板中显示特定记录的计算结果。这也将在用户界面(尚未构建)上复制。

我已经学习了变量的基本存储和操作,以便根据基本用户输入进行其他计算。这有点不同,因为它是在管理面板中完成的。我正在使用PyCharm,但PyCharm告诉我有错误。我还使用此处的链接查看是否可以正常使用

https://www.reddit.com/r/django/comments/6xigr3/how_to_show_calculated_field_in_django_admin/

我意识到下面的代码不正确,但是也不确定我是否正确引用了变量以从主函数中访问它们?

在管理面板和用户界面中使用时,定义此功能的方式有何变化?

谢谢您对以上任何问题的帮助!

# Create a class to calculate the volume by first converting inches to ft, then to cubic yd
class Volume(models.Model):
    linear_ft = models.DecimalField('Length in feet: ', max_digits=200, decimal_places=2)
    depth_in = models.DecimalField('Depth in inches: ', max_digits=200, decimal_places=2)
    width_in = models.DecimalField('Width in inches: ', max_digits=200, decimal_places=2)

    # Convert inches to feet, store variables for use in future functions
    def in_to_ft(self):
        depth_ft = self.depth_in * 12
        width_ft = self.width_in * 12

    # Call to get cubic yards which uses variables from previous functions
    def get_cu_yd(self):
        dims = str(self.linear_ft) + " length x " + str(self.depth_ft) + " depth x " + str(self.width_ft) + " width")
        cubic_ft = self.linear_ft * self.depth_ft * self.width_ft
        cubic_yd = cubic_ft * 0.037037

        return cubic_yd + "cubic yards: " + str(self.dims)

    # Register the function to the property method
    obj_cu_yd = property(get_cu_yd)

def __str__(self):
    return obj_cu_yd

我想接受用户输入(可以为小数),将英寸转换为英尺,乘以长度(ft)x宽度(ft)x深度(ft)来计算立方英尺,并存储一个字符串'版本以显示对象的尺寸。 str (自身)应返回立方码和描述对象尺寸的字符串。

:: UPDATE :: 我已经走了这么远:

# Create a class to calculate the volume by first converting inches to ft, then to cubic yd

类卷(models.Model):     名称= models.CharField(max_length = 200)     linear_ft = models.DecimalField('英尺长度:',max_digits = 200,decimal_places = 2,默认= 0)     depth_in = models.DecimalField('以英寸为单位的深度:',max_digits = 200,decimal_places = 2,default = 0)     width_in = models.DecimalField('以英寸为单位的宽度:',max_digits = 200,decimal_places = 2,默认值= 0)

# Create empty variables so not to divide/multiply by nonzero
cubic_ft = 0
cubic_yd = 0

# Set conversion rate from ft to yards
cubic_ft_yd = 0.037037

# Call to get cubic yards
def get_cu_yd(self):
    depth_ft = decimal.Decimal(self.depth_in) * 12
    width_ft = decimal.Decimal(self.width_in) * 12
    dims = str(self.linear_ft) + " length x " + str(depth_ft) + " depth x " + str(width_ft) + " width"
    cubic_ft = decimal.Decimal(self.linear_ft)*depth_ft*width_ft
    cubic_yd = cubic_ft*self.cubic_ft_yd

    return str(self.name) + " - " + str(cubic_yd) + "cubic yards: " + dims

# Register the function to the property method
obj_cu_yd = property(get_cu_yd)

def str (自己):     返回obj_cu_yd

现在我的问题是此错误: 不支持的操作数类型:'decimal.Decimal'和'float' 参照cube_yd = cubic_ft self.cubic_ft_yd

预先感谢

1 个答案:

答案 0 :(得分:1)

您不应像这样设置depth_ftwidth_ft。您需要改为设置self.depth_ft = self.depth_in * 12,以便该变量实际上被注册为实例的成员。

实际上,您根本不应该具有该辅助功能。

def get_cu_yd(self):
        depth_ft = self.depth_in * 12
        width_ft = self.width_in * 12
        dims = str(self.linear_ft) + " length x " + str(depth_ft) + " depth x " + str(width_ft) + " width")
        cubic_ft = self.linear_ft * depth_ft * width_ft
        cubic_yd = cubic_ft * 0.037037

        return dims + "cubic yards: " + str(self.dims)