Django field for storing dates without years.
Project description
Provides a Django model and form fields for dates that do not include years.
One use case is to collect birthdays without requiring the year of birth.
Prerequisites
Django 1.8 - 1.11
Python 2.7
(Other versions may be supported, but are untested.)
Installation
pip install django-yearlessdate
Usage
The package provides two model fields, YearlessDateField and YearField.
YearlessDateField
YearlessDateField stores a date without a year: January 1st, for example.
Its default widget consists of two dropdowns, one for a day and one for the month.
It will only allow potentially valid dates. For example, a user won’t be able to set the date to April 31st. February 29th is counted as a valid date.
Here’s an example models.py that declares a model with a required yearless date:
from django.db import models from djangoyearlessdate.models import YearlessDateField class MyModel(models.Model): birthday = YearlessDateField()
The values of YearlessDateField on the model instances can be accessed like so:
>>> a = MyModel.objects.get(id=1) >>> a <MyModel: 4 August 2011> >>> a.birthday.day 4 >>> a.birthday.month 8 >>> print a.birthday 4 August
They can also be compared or sorted as would be expected, for example:
>>> m = MyModel.objects.all() >>> m [<MyModel: 4 August 2011>, <MyModel: 30 June 2013>] >>> m[0].birthday > m[1].birthday True >>> m.order_by('birthday') [<MyModel: 30 June 2013>, <MyModel: 4 August 2011>]
YearField
YearField is a very simple model field that stores the year as an integer, and ensures the year provided lies between 1900 and 2200:
from django.db import models from djangoyearlessdate.models import YearField class MyModel(models.Model): year = YearField(null=True, blank=True)
Use of YearField is not recommended due to its lack of configurability. You’re almost certainly better of using a SmallIntegerField in combination with a MinValueValidator and a MaxValueValidator.
Running tests
Setup
Ensure you have a recent version of tox installed.
Clone this repo.
Running tests
From the root of this repo, simply run:
tox
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-yearlessdate-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0327c84300a46e91aa71202883016de54ab1c411c92e5570edb2aefdd2b865a |
|
MD5 | 2b7ed4e37f5667a1b66eeb1d4b84c2a2 |
|
BLAKE2b-256 | 4ced82868571d15260b32b3919db754ec4d52bb4fba5eef2ac395e80899db1d1 |
Hashes for django_yearlessdate-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47df346104bd8b5bb13c8ceea058cd5c197cffc66a03ff57a916d1493abe4bdb |
|
MD5 | 1e28052a185d7c450f742092b272d047 |
|
BLAKE2b-256 | 232328862d0f9d130970e7884bf26d78e1ebbb8617857f8d4aa5d429a39722a5 |