Skip to main content

A calendar model with date metadata for querying against.

Project description

A calendar model with date metadata for querying against.

Sometimes it is useful to have a range of dates and associated metadata stored in the database, rather than calculating on-the-fly, particularly when creating reports based on quarters, fiscal years, etc. This is where django-calendardate comes in. Also known as a date dimension table or calendar table, the inspiration for this project comes from this article.

django-calendardate provides:

  • a model for storing metadata about a set of dates.

  • a management command for generating the metadata

THIS PROJECT IS PRE_RELEASE. It works, but is missing documentation and tests. Be cautious.

Usage

To process a range of dates (ignoring any duplicates):

python manage.py process_dates 2021-01-01 2021-12-31

To process a range of dates (overwriting any duplicates):

python manage.py process_dates 2021-01-01 2021-12-31 --force

Optional settings.py settings

By default, django-calendardate assumes a fiscal year starts in October. If your fiscal year begins on a different month, set FISCAL_YEAR_START_MONTH to the month number (1-indexed). For instance January would be 1).

FISCAL_YEAR_START_MONTH = 1

Model Fields

Each of the following are automatically calculated and inserted into the model using the process_dates management command.

calendar_date (DateField)

The actual date object

calendar_day (PositiveSmallIntegerField)

Number from 1 through 31

calendar_month (PositiveSmallIntegerField)

Month number from 1-12

calendar_year (PositiveSmallIntegerField)

Current year, eg: 2017, 2025, 1984

calendar_quarter (PositiveSmallIntegerField)

1-4, indicates quarter within the current year

fiscal_year (PositiveSmallIntegerField)

Current fiscal year, eg: 2017, 2025, 1984

fiscal_quarter (PositiveSmallIntegerField)

1-4, indicates fiscal quarter within the current fiscal year

day_of_week (PositiveSmallIntegerField)

Monday is 0 and Sunday is 6

day_of_isoweek (PositiveSmallIntegerField)

Monday is 1 and Sunday is 7

day_of_quarter (PositiveSmallIntegerField)

Number from 1-92, indicates the day # in the current quarter

day_of_year (PositiveSmallIntegerField)

Number from 1-366

week_of_month (PositiveSmallIntegerField)

Number from 1-6, indicates the number of week within the current month

week_of_year (PositiveSmallIntegerField)

Number from 1-53, indicates the number of week within the current year

isoweek_of_year (PositiveSmallIntegerField)

Number from 1-53, indicates the number of isoweek within the current year

is_weekday (BooleanField)

True if Monday–>Friday, False for Saturday/Sunday

is_leap_year (BooleanField)

True if current year is a leap year

days_in_month (PositiveSmallIntegerField)

Number of days in the current month

Model Properties

Each of the following properties are provided for each date.

get_day_name (property)

returns a string with the name of the day for the given date (e.g.: “Monday”)

get_month_name (property)

returns a string with the name of the month for the given date (e.g.: “January”)

get_month_abbreviated (property)

returns a string with the abbreviated name of the month for the given date (e.g.: “Jan”)

Quick Example:

Say you have an Order model with a order_date field, and you want to query all of the orders that were placed in the third fiscal quarter of fiscal year 2021.

# Return list of  of dates in 3rd Qtr of FY21
third_fiscal_qtr_dates = CalendarDate.objects.filter(fiscal_year=2021, fiscal_quarter=3).values_list('calendar_date', flat=True)

# Filter on those dates
third_qtr_orders = Order.objects.filter(order_date__in=third_fiscal_qtr_dates)

To Do

  1. Testing

  2. Better documentation

  3. Translations (strings already marked for translation)

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-calendardate-0.1.4.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

django_calendardate-0.1.4-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file django-calendardate-0.1.4.tar.gz.

File metadata

  • Download URL: django-calendardate-0.1.4.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.6

File hashes

Hashes for django-calendardate-0.1.4.tar.gz
Algorithm Hash digest
SHA256 84de1a57616965403f3a23108ccabb3ec08024c92aa541b16e3b0022d5bd4240
MD5 8427c5e616062468826fbe21cf1187d1
BLAKE2b-256 bcef01e3619335330138103ae8c72ac7ee5b48a1d9453e9ac0617fda5f3c2954

See more details on using hashes here.

File details

Details for the file django_calendardate-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: django_calendardate-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.6

File hashes

Hashes for django_calendardate-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8c48de0ba564031ac9269c08a622ccd6e2cd752c46bfe3a5a0b152f8262468b2
MD5 224253364690fd61d03b05c748d93d26
BLAKE2b-256 c2d07ef888634c79478d9e6deb404d2efd6f6bd91ce8610cc5968fbe3ca3c401

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