Skip to main content

TimedeltaField for django models

Project description

PostgreSQL can store data as INTERVAL type, which is close to meaning the same as python’s timedelta object (although better in a couple of ways).

I have lots of use for timedelta objects, and having code that basically wrapped integer objects as a number of seconds was common. This module combines the two:

  • a timedelta.TimedeltaField() object that transparently converts to and from datetime.timedelta

  • storage of the data as an INTERVAL in PostgreSQL, or a string in other databases. (Other databases will be considered if I ever use them, or receive patches).

The coolest part of this package is the way it manipulates strings entered by users, and presents them. Any string of the format:

[X weeks,] [Y days,] [Z hours,] [A minutes,] [B seconds]

will be converted to a timedelta object. Even shortened versions can be used: hrs, hr or h will also suffice. The parsing ignores trailing ‘s’, but is smart about adding them in when presenting the data to the user.

To use, install the package, and use the field:

from django.db import models
import timedelta

class MyModel(models.Model):
    the_timedelta = timedelta.fields.TimedeltaField()

There are also some useful methods in helpers.py to work with timedelta objects. (eg, multiply, divide, modulo and percentages).

Additionally, there are two template filters, timedelta and iso8601, which will convert a timedelta object into a valid string.

Examples

Event model:

from django.db import models
import timedelta

class Event(models.Model):
    start = models.DateTimeField()
    duration = timedelta.fields.TimedeltaField()

    @property
    def finish(self):
        return self.start + self.duration

Storing data within the field:

evt = Event.objects.create(
    start=datetime.datetime.now(),
    duration=datetime.timedelta(hours=1)
)
print evt.finish

evt.duration = datetime.timedelta(minutes=3)
print evt.finish

# We can use valid strings of the format described above.
Event.objects.update(duration='2 hours, 45 minutes')

evt = Event.objects.get(pk=evt.pk)
print evt.finish

# We can also assign directly to this field a valid string.
evt.duration = '3 days, 2 hours'
evt.save()

# Note: we need to re-fetch to ensure conversion to timedelta
evt = Event.objects.get(pk=evt.pk)
print evt.finish

Using with a form.

You can just use a ModelForm, and it will automatically select the TimedeltaFormField, which will handle the conversion between formatted strings and timedelta objects.

Or you may use the form field directly:

from django import forms
import timedelta

class EventForm(forms.Form):
    start = forms.DateTimeField()
    duration = timedelta.forms.TimedeltaFormField()

This form field will display a value in the format:

2 day, 3 hours, 1 minute

And will parse data from a similar format.

Have a look in tests.py for examples of the form field/widget output.

Todo

Parse ISO8601 strings. Thanks to Guillame Libersat, we can generate them.

Handle strings with times in other languages. I’m not really sure about how to do this, but it may be useful.

Changelog

0.6.3: Correctly parse ‘1h’ as one hour (previously required 1hr).

0.6.2: Remember to include VERSION number.

0.6.0: Added total_seconds helper (for python < 2.7)

0.5.3: Include long_description from this file.

0.5.2: Added decimal_percentage, which gives us a decimal.Decimal object.

0.5.1: Bugfixes from Mike Fogel.

0.5: Feature from Guillaume Libersat: helper and template for ISO8601 representation.

Bugfix from croepha: allow for non-plural ‘days’ string. Bugfix from Guillaume Libersat: don’t explode if initial is None

0.4.7: Bugfix from savemu: use unicode() instead of str()

0.4.6: Add in support for PostGIS database.

Make it easier to add in other column types for other databases.

0.4.5: Restore functionality for django <1.2 (thanks Yoav Aner).

0.4.3: Added helpers.modulo, to allow remainder division of timedlelta objects.

0.4.1: changed get_db_prep_value() code to be in get_prep_value(), since I

was calling it in get_default(), without a connection value.

0.4: added the connection and prepared arguments to get_db_prep_value(),

so that django 1.3+ will not complain of DeprecationWarnings.

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-timedeltafield-0.6.3.tar.gz (13.5 kB view details)

Uploaded Source

File details

Details for the file django-timedeltafield-0.6.3.tar.gz.

File metadata

File hashes

Hashes for django-timedeltafield-0.6.3.tar.gz
Algorithm Hash digest
SHA256 f75e247aae7713a8d57614e3dd65cb1a83a1d88c5ecec0efdb4cb56ddb720c10
MD5 7e6fd7d58bbdfe52086b605260125850
BLAKE2b-256 4317e95b84a9cbf3887f27e3560d63d2c75d1ca22d9ef944ddda9dc0ef517395

See more details on using hashes here.

Supported by

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