Skip to content

Django specialized model field inherited from FloatField with unit conversion by custom widget

License

Notifications You must be signed in to change notification settings

mbourqui/django-unitfield

 
 

Repository files navigation

Python Django License PyPI Build Status Coverage Status

django-unit-field

Django specialized model field inherited from FloatField with unit conversion by custom widget

Installation

You will need to add unit_field to your INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    # ...
    'unit_field',
)

Abstract

django-unit-field provides a set of Fields for your model. Each field will create three columns in the corresponding database that holds all required informations. These classes are located in unit_field.fields:

* LengthField (base unit: m=1)
* SquareMeasureField (base unit: m²=1)
* SolidMeasureField (base unit: m³=1)
* MassField (base unit: g=1)
* TimeField (base unit: s=1)
* TemperatureField (base unit: K=1)
* AmountOfSubstanceField (base unit: mol=1)
* LuminousIntensityField (base unit: cd=1)

Enhance your models

Example:

from unit_field.fields import SolidMeasureField, TemperatureField

class Engine(models.Model):
    cubic_capacity = SolidMeasureField(
        verbose_name=u'cubic capacity')
    operating_temperature = TemperatureField(
        verbose_name=u'operatiing temperature')

Enable client-side unit conversion

If you want to use client-side unit conversion, you have to include the static javascript file located in the static directory of the app:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/{{jquery-version}}/jquery.min.js"></script>
<script src="{{ STATIC_URL }}js/jquery.unitfield.js"></script>

If you want to exclude some fields from automatic conversion, you can use the additional parameter auto_convert:

class Engine(models.Model):
    ...
    operating_temperature = TemperatureField(auto_convert=False,
        verbose_name=u'operatiing temperature')

Declare custom units

TODO: Describe how to inherit from class UnitField.

About

Django specialized model field inherited from FloatField with unit conversion by custom widget

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • Python 91.2%
  • Shell 5.8%
  • JavaScript 3.0%