https://github.com/ktowen/django_partial_date
Django custom model field for partial dates with the form YYYY, YYYY-MM, YYYY-MM-DD
https://github.com/ktowen/django_partial_date
Last synced: 3 days ago
JSON representation
Django custom model field for partial dates with the form YYYY, YYYY-MM, YYYY-MM-DD
- Host: GitHub
- URL: https://github.com/ktowen/django_partial_date
- Owner: ktowen
- License: mit
- Created: 2017-10-13T17:55:55.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-01-20T20:13:54.000Z (about 3 years ago)
- Last Synced: 2024-09-30T22:11:43.692Z (7 months ago)
- Language: Python
- Homepage:
- Size: 21.5 KB
- Stars: 23
- Watchers: 2
- Forks: 8
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- cuban-opensource - django-partial-date - MM, YYYY-MM-DD. (Web Development / Python)
README
django_partial_date
================Django custom model field for partial dates with the form YYYY, YYYY-MM, YYYY-MM-DD
* Works with DRF
* Supports comparison operations
* Supports Django 2.0 and Django 3.0Usage
================install the package
```bash
pip install django_partial_date
```## partial_date.PartialDateField
A django model field for storing partial dates. Accepts None, a partial_date.PartialDate object, or a formatted string such as YYYY, YYYY-MM, YYYY-MM-DD. In the database it saves the date in a column of type DateTimeField and uses the seconds to save the level of precision.
## class partial_date.PartialDate
Object to represent the partial dates.
## Example
models.py
```python
from django.db import models
from partial_date import PartialDateFieldclass TestModel(models.Model):
some_partial_date = PartialDateField()
``````python
>>> from partial_date import PartialDate
>>> from core.models import TestModel
>>> obj = TestModel(some_partial_date="1995")
>>> obj.save()
>>> obj.some_partial_date
'1995'
>>> obj.some_partial_date = PartialDate("1995-09")
>>> obj.save()
>>> obj.some_partial_date
1995-09
>>>
``````python
>>> from partial_date import PartialDate
>>> import datetime
>>> partial_date_instance = PartialDate(datetime.date(2012, 9, 21), precision=PartialDate.DAY)
>>> partial_date_instance
2012-09-21
>>> partial_date_instance.precisionYear()
False
>>> partial_date_instance.precisionMonth()
False
>>> partial_date_instance.precisionDay()
True
>>> partial_date_instance.precision == PartialDate.YEAR
False
>>> partial_date_instance.precision == PartialDate.MONTH
False
>>> partial_date_instance.precision == PartialDate.DAY
True
>>> partial_date_instance.precision = PartialDate.MONTH
>>> partial_date_instance
2012-09
>>> partial_date_instance = PartialDate("2015-11-01")
>>> partial_date_instance.date
datetime.date(2015, 11, 1)
``````python
>>> from partial_date import PartialDate
>>> partial_date_instance = PartialDate("2015-11-01")
>>> partial_date_instance
2015-11-01
>>> partial_date_instance.format('%Y', '%m/%Y', '%m/%d/%Y') # .format(precision_year, precision_month, precision_day)
'11/01/2015'
>>> partial_date_instance = PartialDate("2015-11")
>>> partial_date_instance
2015-11
>>> partial_date_instance.format('%Y', '%m/%Y', '%m/%d/%Y')
'11/2015'
>>> partial_date_instance = PartialDate("2015")
>>> partial_date_instance
2015
>>> partial_date_instance.format('%Y', '%m/%Y', '%m/%d/%Y')
'2015'
```Thanks for their collaborations to
- lorinkoz
- howieweiner
- jghyllebert