Skip to main content
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).

Metadata

Release files for django-relativedelta 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-relativedelta 2.0.0
File Size Uploaded Provenance Actions
django-relativedelta-2.0.0.tar.gz 14.2 kB Without provenance Details for django-relativedelta-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-relativedelta 2.0.0
File Interpreter ABI Platform Actions
django_relativedelta-2.0.0-py3-none-any.whl Python 3 none any Details for django_relativedelta-2.0.0-py3-none-any.whl

Total release size: 22.3 kB

Release files / django-relativedelta-2.0.0.tar.gz

Download URL django-relativedelta-2.0.0.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8a56bd5461dea35b74011f95be1d90d20caafe1f0c9be2fce8e8eefdf92e2cbe
BLAKE2b-256 checksum
How to use checksums
fc82cc3910164559a063e9bf3cdb4214ed57dbaa1395cd9ad37b4b958997accb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / django_relativedelta-2.0.0-py3-none-any.whl

Download URL django_relativedelta-2.0.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72b37b27eadc03b0b4869712363c37126445161f6880106468006e4b52a76e59
BLAKE2b-256 checksum
How to use checksums
913a0636c196b3e1965b8497064e45f55719ddf14f04b6e11ad4d5057da40346
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Security for django-relativedelta 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page