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 with choices option, that determines what this quantity means (length, time duration etc).
Should I create a model for units and physical quantity or should I use IntegerField that contains the 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 something like this to keep track of conversions.
Given this mapping, you can get a conversion factor in your conversion method function, do the math, and return the converted unit.
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 various 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 Imperial units to load into your table (fluid ounces, teaspoons, tablespoons, cups, pints, quarts, etc.)