I am facing problem in my django app while working with
from django.db import models
from datetime import timedelta
name = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField()
def copy_event(self, add_days=timedelta(days=7)):
start_date = self.start_date + add_days
name = self.name +' (copy)'
new_event = Event(name = name,created = self.created,start_date=start_date,)
USE_TZ=True you are always dealing with aware datetimes, which represent a precise moment in time. That moment in time will, in general, be displayed differently depending on the current time zone. Some time zones have DST and some don't, so in general there's no way you can store the datetime such that all users in all timezones will see the clock time you want (i.e. the same clock time as the event being copied).
I can think of a couple workarounds:
If it's the case that you're only serving one time zone (that is, the current time zone is always going to be the same), you can just do your arithmetic in that time zone.
from django.utils.timezone import localtime new_start_date = localtime(self.start_date) + add_days
The addition of
add_days will always keep the clock time the same (that is, it doesn't take DST into account). The
new_start_date will be converted to and stored in UTC, and then converted back to the desired local time for display.
What you're really trying to represent is a particular clock time on a particular day, not an exact moment in time. So perhaps you don't want to be using aware datetimes at all. One option is to set
USE_TZ=False, but that's a global, far-reaching change that you may not want to make. Another option is to use a different representation for this field. For example, you could use a
DateField and a
TimeField instead of a