Giter Site home page Giter Site logo

peymanslh / django-jalali Goto Github PK

View Code? Open in Web Editor NEW

This project forked from slashmili/django-jalali

1.0 2.0 0.0 987 KB

Jalali DateField support for Django model

Home Page: http://pypi.python.org/pypi/django_jalali

License: BSD 3-Clause "New" or "Revised" License

Python 48.00% CSS 0.04% JavaScript 43.79% HTML 8.17%

django-jalali's Introduction

Django Jalali

This module gives you a DateField same as Django’s DateField but you can get and query data based on Jalali Date

Status

https://github.com/slashmili/django-jalali/workflows/Tests/badge.svg?branch=main

Dependencies

  • jdatetime

  • Django > 2.2

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

Install

pip install django_jalali

Usage

  1. Run :
$ django-admin 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 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>]

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

django-jalali's People

Contributors

am1rb avatar aminhp avatar amirfeqhi avatar hramezani avatar m30m avatar meloie avatar mrash14 avatar rezkam avatar sassanh avatar slashmili avatar zaadeh avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.