Skip to main content

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

https://travis-ci.org/slashmili/django-jalali.svg?branch=master https://img.shields.io/pypi/v/django_jalali.svg

Dependencies

  • jdatetime

  • Django > 2.0

    Looking for Django 1.X support? Checkout 2.4.6 version in pypi.org

Install

pip install django_jalali

Usage

  1. Run :

$ django-admin.py startproject jalali_test
  1. Start your app :

$ python manage.py startapp foo
  1. 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

  2. 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)
  1. 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
  1. Test it

$ python manage.py shell
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
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>]

Using Templatetags

  1. You can use jformat filter to format your dates in templates:

{% load jformat %}
{{ my_date|jformat }} {# default formatting #}
{{ my_date|jformat:"%A %d %B %Y %H:%M" }} {# specific formatting #}

Admin Interface

  1. 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)
  1. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-jalali-3.2.1.tar.gz (195.5 kB view details)

Uploaded Source

Built Distribution

django_jalali-3.2.1-py3-none-any.whl (214.2 kB view details)

Uploaded Python 3

File details

Details for the file django-jalali-3.2.1.tar.gz.

File metadata

  • Download URL: django-jalali-3.2.1.tar.gz
  • Upload date:
  • Size: 195.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for django-jalali-3.2.1.tar.gz
Algorithm Hash digest
SHA256 a582f3603119b49ba18fc1f719afceca004f213059647160458178bb4e57b625
MD5 f26c3b55a3f62c8ad6c4d607ec49fe63
BLAKE2b-256 6fe8ad2945f875a3f5d4063b0e040f30b0d7813061240529259e90243106999c

See more details on using hashes here.

File details

Details for the file django_jalali-3.2.1-py3-none-any.whl.

File metadata

  • Download URL: django_jalali-3.2.1-py3-none-any.whl
  • Upload date:
  • Size: 214.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for django_jalali-3.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0efbc816c17c4ae2c8c241d8fe4c60764e0f338bfc494bd6ffc86920f1b4fc19
MD5 0cf9f332981d538a5003355ef16272f7
BLAKE2b-256 bd8688cbb89981bbbea8193246f3d17ce2334c1064ca90136abd1b014820b5df

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