zako
zako

Reputation: 395

Use different timezone for forms in django's admin site

I wonder if it is possible for django's admin site to accept two datetime fields of one model in two different timezones.

For example suppose I have a model like:

class A(models.Model):
    time_a = models.DateTimeField()
    time_b = models.DateTimeField()

When I use the django's admin site to add or edit an instance of A, I wish I may fill field time_a in UTC+8, and time_b in UTC+9. i.e. If I fill time_a with 08:00 and time_b with 09:00, they are the same in UTC.

I know that I may override A's save() method and handle inputs manually with doing the timedelta calculation myself, but I wonder if there is a more convenient way or more proper way to do this.

Upvotes: 3

Views: 364

Answers (1)

Juca
Juca

Reputation: 489

I think you could extend the django's DateTimeField (https://github.com/django/django/blob/master/django/db/models/fields/init.py#L1332), changing its init to accept one more argument default_timezone and alter to_python method (which is inherited from DateField https://github.com/django/django/blob/master/django/db/models/fields/ init.py#L1257) to use that default_timezone.

I haven't tested that

EDIT: What you see as a bold init in fact is a dunder init (init with two underscores before and two underscores after). I just don't realized how to disable markup here in stackoverflow.

Upvotes: 1

Related Questions