Skip to content

Latest commit

 

History

History
53 lines (30 loc) · 1.94 KB

README.md

File metadata and controls

53 lines (30 loc) · 1.94 KB

django-variable-resolution-date

Build Status PyPI version Requirements Status License: MIT

A django field that can represent either a year, or a year and a month, or a full calendar date.

This is useful when the precision of your data varies (perhaps your legacy data only contains a year whereas you now start collecting full dates).

The representation in the database is simply a string in https://en.wikipedia.org/wiki/ISO_8601 format. This means we cannot use date-specific database operations, but we can still leverage the fact that the lexicographical order corresponds to the chronological order:

  • Sorting on a VariableResolutionDateField column will sort chronologically.
  • We can filter for dates within a range by using string operations, for instance: member_since > '2000'

Requirements

  • Python: 3.4 and over
  • Django: 1.10 and over

Usage

Install from pip:

pip install django-variable-resolution-date

Use in your models:

from django.db import models
from variable_resolution_date import VariableResolutionDateField


class ClubMember(models.Model):
    name = models.CharField(max_length=100)
    member_since = VariableResolutionDateField()

The input widget for such fields is a text field that verifies the input is valid. For instance:

  • 1996, 1996-02, 1996-02-29 are valid
  • 1997-02-29 is invalid