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



我是Python的新手,并且正在研究一个新的Django项目,并希望使用记录数据在管理面板中以特定记录显示计算。这也将在用户界面上复制(尚未构建(。

我已经学习了基本的基本变量的存储和操纵,以通过基本用户输入进行的其他计算。这有点不同,因为它在管理面板内完成。我正在使用Pycharm,但Pycharm告诉我有错误。我还在这里使用了链接,看看我是否可以使它起作用:

https://www.reddit.com/r/django/comments/6xigr3/how_to_show_show_calcuald_field_inf_in_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(x depth(ft(来计算立方英尺,然后存储一个'字符串'显示对象的尺寸的版本。 str (self(应返回立方码和描述对象尺寸的字符串。

::更新::我走了这么远:

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

类卷(Models.Model(: name = models.charfield(max_length = 200( linear_ft = model.decimalfield('长度为脚:',max_digits = 200,decimal_places = 2,默认= 0( depth_in = model.decimalfield('depth In Inches:',max_digits = 200,decimal_places = 2,默认值= 0( width_in = models.decimalfield('width in Inches:',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 (self(: 返回obj_cu_yd

现在我的问题是这个错误:的未支撑操作数类型:'Decimal.decimal'和'Float'参考cupic_yd = cupic_ft self.cubic_ft_yd

预先感谢

您不应该像这样设置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)

最新更新