Django and Units Conversion

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

+1


source to share


3 answers


In the "field (enum)" section, do you mean you are using the choices parameter on the field?

A simple set of options works well for small conversion lists. This allows you to simplify the assumptions that will help your users (and you) get something that works.

Creating a formal model for units should only be done if you have (a) many units involved, (b) your need to renew; And (c) there is some rational expectation that the database lookup will be cost.

The units don't change often. There is no reason to use a database for this. It seems much easier to hard-code the list of options.

Choice

You can, for example, use something like this for conversion tracking.

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 the conversion factor in your conversion function, do the math, and return the converted block.

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 must carry around the kind of measurement (length, volume, mass, weight / force, pressure, temperature, etc.) and the unit conversion factors. This works for everything except temperature, where you have a constant term in addition to the coefficient.

You need to choose a "basic" set of units (eg MKS ) and carry all the factors between the different units.

You also need to choose how many imperial units to load into your table (fluid ounces, teaspoons, tablespoons, cups, pints, quarts, etc.).

+4


source


It depends on how you want to use it. Let's say you have a length value and two possible units, cm and mm. If you only want to print the value later, you can always print it as a value.

However, if you want to do some calculations on a value, such as calculating the area, you need to convert the values ​​to the same units. Therefore, you still need to define a unit conversion table.



I would convert the units to the same inner block before storing them in the database, rather than converting them every time I use them.

I would add a model for units and physical quantities only if there are too many of them and the conversion is really difficult. Such a model can act as a converter. But for simple cases such as mm⇒cm or inch⇒cm, a static conversion table will suffice.

+1


source


Use a field that indicates the type of measure (weight, length, etc.) and store the value in another field. That should be enough. The unit of measurement must be implicit. I am assuming you are using the same unit of measure for each type of measurement, for example always meters of length.

A concrete example: let's say you have two objects: Car and CarMeasures . I would write the model like this:

class Car(models.Model):
    type=models.CharField(max_length=256);

class CarMeasures(models.Model):
    carId=models.ForeignKey(Car);
    measureValue=models.DecimalField(..., max_digits=10, decimal_places=2);
    measureType=models.CharField(max_length=32);

      

+1


source







All Articles