Skip to content

Form and model fields for storing durations on Django models as ISO 8601 compliant strings, and returning relativedelta objects.

License

Notifications You must be signed in to change notification settings

thecut/thecut-durationfield

Repository files navigation

Welcome to thecut-durationfield

Documentation Status

This app provides a custom Django model field, RelativeDeltaField, and related form fields and widgets. RelativeDeltaField stores time durations using ISO 8601 representations, and returns dateutil.relativedelta objects which may be used directly with datetime.datetime objects.

This project was inspired by packages such as django-durationfield. However, this project focuses on:

  1. providing a database-agnostic, standards-compliant way of storing the durations in the database (using ISO 8601).
  2. returning dateutil.relativedelta objects that can be used to perform calculations on datetime.datetime objects.

Note that django-durationfield provides the ability to filter querysets based on the relative size of the stored duration, which is not possible with this project. I.e., you can't use __lt and __gt etc., when filtering by fields provided by this project.

Documentation

The full documentation is at https://thecut-durationfield.readthedocs.org.

Quickstart

Install thecut-durationfield using the installation instructions found in the documentation.

Model field

from django.db import models
from datetime import datetime
from thecut.durationfield.models import RelativeDeltaField


class MyModel(models.Model):
    duration = RelativeDeltaField(blank=True, null=True)


my_instance = MyModel(duration='P7D')
datetime(2014, 1, 1) + my_instance.duration  # datetime(2014, 1, 8, 0, 0)

Form field

Two form fields are provided: RelativeDeltaChoiceField and RelativeDeltaTextInput:

from django import forms
from thecut.durationfield.models import RelativeDeltaChoiceField

DURATIONS = [
    ('', 'Never'),
    ('P7D', 'One week'),
    ('P1M', 'One month'),
]

class MyForm(forms.ModelForm):

    duration = RelativeDeltaChoiceField(choices=DURATIONS)

or, if you'd prefer to type in the (ISO 8601 compliant) value manually:

from django import forms
from thecut.durationfield.forms import RelativeDeltaTextInput

class MyForm(forms.ModelForm):

    duration = RelativeDeltaTextInput()

Credits

See AUTHORS.rst.

About

Form and model fields for storing durations on Django models as ISO 8601 compliant strings, and returning relativedelta objects.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages