Jalali Date support for Django model and admin interface
Project description
This module gives you a DateField same as Django’s DateField but you can get and query data based on Jalali Date
Status
Dependencies
Install
pip install django_jalali
Usage
Run :
$ django-admin.py startproject jalali_test
Start your app :
$ python manage.py startapp foo
Edit settings.py and add django_jalali and your foo to your INSTALLED_APPS (also config DATABASES setting)
django_jalali should be added before your apps in order to work properly
Edit foo/models.py
from django.db import models
from django_jalali.db import models as jmodels
class Bar(models.Model):
objects = jmodels.jManager()
name = models.CharField(max_length=200)
date = jmodels.jDateField()
def __str__(self):
return "%s, %s"%(self.name, self.date)
class BarTime(models.Model):
objects = jmodels.jManager()
name = models.CharField(max_length=200)
datetime = jmodels.jDateTimeField()
def __str__(self):
return "%s, %s" %(self.name, self.datetime)
Run
$ python manage.py makemigrations
Migrations for 'foo':
foo/migrations/0001_initial.py:
- Create model Bar
- Create model BarTime
$ python manage.py migrate
Running migrations:
Applying foo.0001_initial... OK
Test it
$ python manage.py shell
Python 3.7.0 (default, Nov 26 2018, 15:26:54)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from foo.models import Bar
>>> import jdatetime
>>> today = jdatetime.date(1390, 5, 12)
>>> mybar = Bar(name="foo", date=today)
>>> mybar.save()
>>> mybar.date
jdatetime.date(1390, 5, 12)
>>> Bar.objects.filter(date=today)
[<Bar: foo, 1390-05-12>]
>>> Bar.objects.filter(date__gte="1390-5-12")
[<Bar: foo, 1390-05-12>]
>>> Bar.objects.filter(date='1363-8-01')
[]
>>> from foo.models import BarTime
>>> BarTime(name="Bar Time now", datetime=jdatetime.datetime(1380,8,2,12,12,12)).save()
>>> BarTime.objects.filter(datetime__lt= jdatetime.datetime(1380,8,2,12,12,12 ))
[]
>>> BarTime.objects.filter(datetime__lte= jdatetime.datetime(1380,8,2,12,12,12 ))
[<BarTime: Bar Time now, 1380-08-0212:12:12>]
>>> BarTime.objects.filter(datetime__gt='1380-08-02')
[<BarTime: Bar Time now, 1380-08-0212:12:12>]
>>> BarTime.objects.filter(datetime__gt=d)
[]
>>> BarTime.objects.filter(datetime__year=1380)
[<BarTime: Bar Time now, 1380-08-0212:12:12>]
Admin Interface
Create foo/admin.py
from foo.models import Bar,BarTime
from django.contrib import admin
from django_jalali.admin.filters import JDateFieldListFilter
#you need import this for adding jalali calander widget
import django_jalali.admin as jadmin
class BarAdmin(admin.ModelAdmin):
list_filter = (
('date', JDateFieldListFilter),
)
admin.site.register(Bar, BarAdmin)
class BarTimeAdmin(admin.ModelAdmin):
list_filter = (
('datetime', JDateFieldListFilter),
)
admin.site.register(BarTime, BarTimeAdmin)
Config admin interface and fire up your django and enjoy using jalali date !
Locale
In order to get the date string in farsi you need to set the locale to fa_IR
There are two ways to do achieve that, you can use of the approaches based on your needs
Run server with LC_ALL env:
$ LC_ALL=fa_IR python manage.py runserver
Set the locale in settings.py
LANGUAGE_CODE = 'fa-ir'
import locale
locale.setlocale(locale.LC_ALL, "fa_IR.UTF-8")
Timezone Settings
From django_jalali version 3 and Django 2 you can use TIME_ZONE and USE_TZ settings to save datetime with project timezone
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
File details
Details for the file django-jalali-4.1.2.tar.gz
.
File metadata
- Download URL: django-jalali-4.1.2.tar.gz
- Upload date:
- Size: 197.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99fffbc449c58648ca85297d31592a86190e8d7d8c60ed8b28ac3351e764124c |
|
MD5 | fa988584cd3eaa6551d5f12d476d1cf3 |
|
BLAKE2b-256 | b66804289af12bbaf5e68e01d9d1ac96cb2da194e44d4d004affff9be0bfafa7 |
File details
Details for the file django_jalali-4.1.2-py3-none-any.whl
.
File metadata
- Download URL: django_jalali-4.1.2-py3-none-any.whl
- Upload date:
- Size: 214.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9bec9933f842c9a2ff5c459bea4b087dd808e85a1d1aee597422c8f61dcb771 |
|
MD5 | a39a8d5131f987180d456ac62f3132a9 |
|
BLAKE2b-256 | 1ca8991d6e80157edf3fad776a41fca8342105c62d56fd62bb0efe81a277dc39 |