Skip to main content

Pint Fields for Django and Postgres

Project description

django-pint-field

Use pint with Django's ORM

Modified from the fantastic django-pint with different goals.

Unlike django-pint, in this project we use a composite field to store both the magnitude and value of the field, along with the equivalent value in base units for lookups. For this reason, the project only works with Postgresql databases. It ensures that the units your users want to use are the units they see, while still allowing accurate comparisons of one quantity to another.

Install

pip install django_pint_field

Usage

from decimal import Decimal
from django_pint_field.units import ureg
Quantity = ureg.Quantity

# Start by creating a few Pint Quantity objects
extra_small = Quantity(1 * ureg.gram)
small = Quantity(10 * ureg.gram)
medium = Quantity(100 * ureg.gram)
large = Quantity(1000 * ureg.gram)
extra_large = Quantity(10000 * ureg.gram)

# Create a couple objects
IntegerPintFieldSaveModel.objects.create(name="small", weight=small)
IntegerPintFieldSaveModel.objects.create(name="large", weight=large)

# Perform some queries
IntegerPintFieldSaveModel.objects.filter(weight__gt=medium)
<QuerySet [<IntegerPintFieldSaveModel: large>]>

IntegerPintFieldSaveModel.objects.filter(weight__gt=extra_small)
<QuerySet [<IntegerPintFieldSaveModel: small>, <IntegerPintFieldSaveModel: large>]>

IntegerPintFieldSaveModel.objects.filter(weight__gte=small)
<QuerySet [<IntegerPintFieldSaveModel: small>, <IntegerPintFieldSaveModel: large>]>

IntegerPintFieldSaveModel.objects.filter(weight__range=(small, medium))
<QuerySet [<IntegerPintFieldSaveModel: small>]>

Valid Lookups

Other lookups will be added in the future. Currently available are:

  • exact
  • iexact
  • gt
  • gte
  • lt
  • lte
  • range
  • isnull

Creating your own units

Will be detailed soon

Model Fields

  • IntegerPintField: Stores a pint measurement as an integer (-2147483648 to 2147483647).
  • BigIntegerPintField: Stores a pint measurement as a big integer (-9223372036854775808 to 9223372036854775807).
  • DecimalPintField: Stores a pint measurement as a decimal.

Form Fields

  • IntegerPintFormField: Used in forms with IntegerPintField and BigIntegerPintField.
  • DecimalPintFormField: Used in forms with DecimalPintField.

Widgets

  • PintFieldWidget: Default widget for all django pint field types.

Settings

DJANGO_PINT_FIELD_DECIMAL_PRECISION
Determines whether django_pint_field should automatically set the python decimal precision for the project. If an integer greater than 0 is provided, the decimal context precision for the entire project will be set to that value. Otherwise, the precision remains at the default (usually 28).
* Type: int * Default: 0

Rounding modes (upcoming feature)

decimal.ROUND_CEILING Round towards Infinity.

decimal.ROUND_DOWN Round towards zero.

decimal.ROUND_FLOOR Round towards -Infinity.

decimal.ROUND_HALF_DOWN Round to nearest with ties going towards zero.

decimal.ROUND_HALF_EVEN Round to nearest with ties going to nearest even integer.

decimal.ROUND_HALF_UP Round to nearest with ties going away from zero.

decimal.ROUND_UP Round away from zero.

decimal.ROUND_05UP Round away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise round towards zero.

Read more about rounding modes for decimals at the decimal docs

Use the test app with docker compose

Build and bring up

docker compose build
docker compose run django python manage.py migrate
docker compose run django python manage.py createsuperuser
docker compose up -d

Navigate to 127.0.0.1:8000

Test (assuming you have already performed build)

docker compose run django python manage.py test

Run psql on the Postgres database

docker compose exec postgres psql -U postgres

ToDos:

  • If a unit_choices value is an alias (e.g. pounds vs pound), the form widget will show the incorrect item selected. The correct value is saved in db, though.
  • Implement rounding modes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pint_field-0.1.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pint_field-0.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file django_pint_field-0.1.0.tar.gz.

File metadata

  • Download URL: django_pint_field-0.1.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-56-generic

File hashes

Hashes for django_pint_field-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d4e41792a230c14030593ab231ac6a0d9ce4455ca71fef5e45c7d90daf50f5aa
MD5 21112d03195dae150c7b148e7d3fc24e
BLAKE2b-256 6a314cf2a85af12e8a84f09709b49f4459bc33929ffb9438d5b2a1cf5872b3b5

See more details on using hashes here.

File details

Details for the file django_pint_field-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_pint_field-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-56-generic

File hashes

Hashes for django_pint_field-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08b42d9013d9b9f9dd0c4eb4344c3cd697faeb8f1b0036e6c5b59bbd5145a0db
MD5 e57894156545f3b052cdadea743341b5
BLAKE2b-256 b97fddf1adf3eee0225d7eb4de5574b0ce6f703c244e0e6f15c9713d0d9922fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page