Skip to main content

Django alternative to DurationField using dateutil.relativedelta

Project description

https://travis-ci.org/CodeYellowBV/django-relativedelta.svg?branch=master

A Django field for the dateutil.relativedelta.relativedelta class, which conveniently maps to the PostgreSQL INTERVAL type.

The standard Django DurationField maps to Python’s datetime.timedelta, which has support for days and weeks, but not for years and months. And if you try to read an INTERVAL that contains months anyway, information is lost because each month gets converted to 30 days.

For compatibility, a VARCHAR field is used on other databases. This uses a custom relativedelta representation. However, this means that true in-database interval operations are not supported in these databases. Sorting and comparing between two relativedelta fields or a relativedelta field and a fixed relativedelta value is supported, however.

You should use this package when you need to store payment intervals (which tend to be monthly or quarterly), publication intervals (which can be weekly but also monthly) and so on, or when you simply don’t know what the intervals are going to be and want to offer some flexibility.

If you want to use more advanced recurring dates, you should consider using django-recurrence instead. This maps to the dateutil.rrule.rrule class, but it doesn’t use native database field types, so you can’t perform arithmetic on them within the database.

Usage

Using the field is straightforward. You can add the field to your model like so:

from django.db import models
from relativedeltafield import RelativeDeltaField

class MyModel(models.Model):
  rdfield=RelativeDeltaField()

Then later, you can use it:

from dateutil.relativedelta import relativedelta

rd = relativedelta(months=2,days=1,hours=6)
my_model = MyModel(rdfield=rd)
my_model.save()

Or, alternatively, you can use a string with the ISO8601 “format with designators” time interval syntax:

from dateutil.relativedelta import relativedelta

my_model = MyModel(rdfield='P2M1DT6H')
my_model.save()

For convenience, a standard Python datetime.timedelta object is also accepted:

from datetime import timedelta

td = timedelta(days=62,hours=6)
my_model = MyModel(rdfield=td)
my_model.save()

After a full_clean(), the object will always be converted to a _normalized_ relativedelta instance. It is highly recommended you use the django-fullclean app to always force full_clean() on save(), so you can be sure that after a save(), your fields are both normalized and validated.

Limitations and pitfalls

Because this field is backed by an INTERVAL column, it neither supports the relative weekday, leapdays, yearday and nlyearday arguments, nor the absolute arguments year, month, day, hour, second and microsecond.

The microseconds field is converted to a fractional seconds value, which might lead to some precision loss due to floating-point representation.

The weeks field is “virtual”, being derived from the multiple of 7 days. Thus, any week value in the input interval specification is converted to days and added to the days field of the interval. When serializing back to a string, weeks will never be written. Similarly, if the interval contains a multiple of 7 days, you can read this back out from the weeks property.

Support for databases other than PostgreSQL is limited.

For consistency reasons, when a relativedelta object is assigned to a RelativeDeltaField, it automatically calls normalized() on full_clean. This ensures that the database representation is as similar to the relativedelta as possible (for instance, fractional days are always converted to hours).

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

cval-django-relativedelta-1.1.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

cval_django_relativedelta-1.1.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file cval-django-relativedelta-1.1.2.tar.gz.

File metadata

  • Download URL: cval-django-relativedelta-1.1.2.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for cval-django-relativedelta-1.1.2.tar.gz
Algorithm Hash digest
SHA256 489ba80f08dbc1f7f86eef1bd2ee7f906cd0de50c62a953a50f556a1a8f2ba81
MD5 8590a10cef76d4e6e570bcced0564ed7
BLAKE2b-256 c0e58d5c2c7157e8f86fa012897a0987462d4a409ca51cc9678dace106fb102a

See more details on using hashes here.

File details

Details for the file cval_django_relativedelta-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: cval_django_relativedelta-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for cval_django_relativedelta-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8684228a7f09f02091b4e057da49f1cd9a3f0fadafda658dd52581b07785d051
MD5 9e3e605266d7143aadee12b015d2ef7f
BLAKE2b-256 7ea33fe1d51bfe4dfffb2f90e6bda04252bff735cb280fc0685ef89aed8981f5

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