I would like to implement different systems of measurement in my Django project, so that users can choose wether they want to use metric or imperial units. However, I don't know what is the correct approach to do this.
Currently my models don't have measurement unit aware fields (they're integer/decimal fields) and I would prefer that I don't have to change my fields directly.
Since my current database values already represent metric values, I am planning to keep it that way, which means that I will have to handle user input / value output conversion. Pint seems to be a great library for this purpose.
Is this possible without editing fields in app, but using registration patterns or something else instead? What I would like to achieve is something like this:
- I define a data structure holding different measurement types and possible values, such as length: meters, feet; weight: kilograms, pounds etc.
- I add a new file "measurements.py" or something similar in each app directory, which has fields that hold measurement values. In this field I could then define which are the exact measurement fields and what are their types, such as fields = {mymodelfield: length, myothermodelfield: weight} etc.
- Some default settings could be set in settings file and overwritten in app file, such as default unit for each measurement (unit that is stored in database).
- User sets his preference for each measurement type. There is default unit for each measurement type as mentioned in previous point. Logic to convert user input in case of preference / default (stored) unit mismatch is required. Bound forms should also be able to convert field value back from default to user preferred unit.
This kind of solution would make it easier to plug it to existing apps since no fields and forms would be changed directly in the app. Basic examples using registration patterns would be helpful.
Any relevant information is welcome, including general ideas and patterns how this is done in non-Django projects.