Skip to main content

A Django app providing database and form fields for split datetime/time and pytz timezone objects.

Project description

Here is how it looks like.

Written on top of django-timezone-field.

A Django app providing database and form fields for split datetime/time and pytz timezone objects.

  • Use SplitDateTimeTimeZoneField in place of forms.DateTimeField.

  • Use SplitTimeTimeZoneField in place of forms.TimeField.

The setting USE_TZ_FIELDS = True will convert all admin.ModelAdmin to use SplitDateTimeTimeZoneField for models.DateTimeField and SplitTimeTimeZoneField for models.TimeField.

Requires USE_TZ = True in settings.py

Note

If using a database that does not support timezone-aware times then the initial values of model forms will always be in the UTC timezone.

Form Field

import pytz

from django import http
from django import forms
from django.utils import timezone
from django.forms.util import to_current_timezone
from django.conf import settings

from datetimezone_field import SplitDateTimeTimeZoneField, \
    SplitTimeTimeZoneField


def index(request):

    class MyForm(forms.Form):
        a_datetime = SplitDateTimeTimeZoneField()
        a_time = SplitTimeTimeZoneField()

    tz = pytz.timezone("Australia/Sydney")
    timezone.activate(tz)
    now = to_current_timezone(timezone.now()).replace(tzinfo=tz)

    my_form = MyForm(initial={
        'a_datetime': now,
        'a_time': now.time().replace(tzinfo=now.tzinfo)
    })

    return http.HttpResponse(my_form.as_p())

Installation

  1. From pypi using pip:

    pip install django-datetimezone-field
  2. Add timezone_field and datetimezone_field to your settings.INSTALLED_APPS

INSTALLED_APPS = (
    ...
    timezone_field,
    datetimezone_field,
    ...
)

Running sample project

  1. cd test/sample_project

  2. virtualenv sample-env

  3. source sample-env/bin/activate

  4. pip install -r requirements.txt

  5. python manage.py syncdb

  6. python manage.py runserver

TODO

  1. Unit tests

Found a Bug?

To file a bug or submit a patch, please head over to django-datetimezone-field on github.

Credits

Built on top of Mike Fogel’s django-timezone-field.

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-datetimezone-field-0.1.8.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file django-datetimezone-field-0.1.8.tar.gz.

File metadata

File hashes

Hashes for django-datetimezone-field-0.1.8.tar.gz
Algorithm Hash digest
SHA256 113a3e34e49ab0d036fd57ad0556adf5a6d2be611362f68bab1b9c6be15bbeaa
MD5 3af7d0937aaf73378655b652e50d8f60
BLAKE2b-256 e7a23d19fb6efd83a560cd6f24db8b9ba9fd4726433935581cf91110bef6ee94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page