Django和单位转换 [英] Django and units conversion

查看:120
本文介绍了Django和单位转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在数据库中存储一些值,距离,重量等。在我的模型中,我有一个字段包含某些东西的数量和IntegerField选择选项,这决定了这个数量意味着什么(长度,持续时间等)。我应该创建单位和物理量的模型,还是应该使用包含单位类型的IntegerField?

解决方案

field(enum)你的意思是你正在使用选择选项?



一组简单的选择适用于小型转化列表。它允许您做出简化的假设,帮助您的用户(和您)获得有用的东西。



创建单位的正式模型只能在(a )有很多单位涉及,(b)你需要扩展它,和(c)有一些合理的期望,DB查找将是一些有价值的。



单位不会经常更改。使用数据库似乎没有什么理由。



选择



你可以使用这样的东西跟踪转换。

  UNIT_CHOICES =(('m' ,''''''''''''''''''''''''''''''''''''''''''''''''''''' b('m','f'):3.xyz,
('m','i'):39.xyz,
('m','pt'):29.xyz * 72,
('f','m'):1 / 3.xyz,
('f','i'):12.0,
('f',' '):12.0 * 72,

}

给定这个映射,您可以在转换方法
函数中获得转换因子,做数学,并返回转换的单位。

  class WithUnit(Model):
...
def toUnit(self,someUnit):
如果someUnit == self.unit:return self.value
elif(someUnit, self.unit)在unit_convers离子:
return self.value * unit_conversions [(someUnit,self.unit)]
else:
raise异常(无法转换)

模型



单位的形式模型,你必须承担一些尺寸(长度,体积,质量,重量/力,压力,温度等)和动力单位换算因子。除了温度以外,除了温度外,除了一个因素之外,您还有一个恒定的术语。



您必须选择基准单位(例如, MKS ),并携带各单位之间的所有乘数。



您还必须选择多少英文单位加载到您的桌子(流体盎司,茶匙,汤匙,杯子,品脱,夸脱等)。


I need to store some values in the database, distance, weight etc. In my model, i have field that contains quantity of something and IntegerField whith choices option, that determines what this quantity means(length, time duration etc). Should i create model for units and physical quantity or should i use IntegerField that contains type of unit?

解决方案

By "field(enum)" do you mean you are using the choices option on a field?

A simple set of choices works out reasonably well for small lists of conversions. It allows you to make simplifying assumptions that helps your users (and you) get something that works.

Creating a formal model for units should only be done if you have (a) a LOT of units involved, (b) your need to extend it, AND (c) there's some rational expectation that the DB lookups will be of some value.

Units don't change all that often. There seems little reason to use the database for this. It seems a lot simpler to hard-code the list of choices.

Choices

You can, for example, use a something like this to keep track of conversions.

UNIT_CHOICES = ( ('m', 'meters'), ('f', 'feet' ), ('i', 'inches'), ('pt', 'points') )

unit_conversions = {
    ('m','f'): 3.xyz,
    ('m','i'): 39.xyz,
    ('m','pt'): 29.xyz*72,
    ('f','m'): 1/3.xyz,
    ('f','i'): 12.0,
    ('f','pt'): 12.0*72,
    etc.
}

Given this mapping, you can get a conversion factor in your conversion method function, do the math, and return the converted unit.

class WithUnit( Model ):
    ...
    def toUnit( self, someUnit ):
        if someUnit == self.unit: return self.value
        elif (someUnit,self.unit) in unit_conversions:
            return self.value * unit_conversions[(someUnit,self.unit)]
        else:
            raise Exception( "Can't convert" )

Model.

If you want to create a formal model for units, you have to carry around the kind of dimension (length, volume, mass, weight/force, pressure, temperature, etc.) and the varous unit conversion factors. This works for everything but temperature, where you have a constant term in addition to a factor.

You have to pick a "baseline" set of units (e.g., MKS) and carry all the multipliers among the various units.

You also have to choose how many of the English units to load into your table (fluid ounces, teaspoons, tablespoons, cups, pints, quarts, etc.)

这篇关于Django和单位转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆