django-timezone

Django - How to run a function EVERYDAY?

浪子不回头ぞ 提交于 2019-12-09 06:00:07
问题 I want to run this function everyday midnight to check expiry_date_notification. what can I do? I'm new to django and python. def check_expiry_date(request): products = Product.objects.all() for product in products: product_id = product.id expiry_date = product.expiry_date notification_days = product.notification_days check_date = int((expiry_date - datetime.datetime.today()).days) if notification_days <= check_date: notification = Notification(product_id=product_id) notification.save() 回答1:

return datetimes in the active timezone with a django query

假如想象 提交于 2019-12-09 03:56:49
问题 I am trying to retrieve the last n hour rows from a table and print their datetimes in a given timezone, the timezone to use when printing dates is given, I am trying to use activate to make django return the datetimes with the proper timezone but it returns dates as UTC. here is my current code: min_time = datetime.datetime.now(link.monitor.timezone) - datetime.timedelta(hours=period) timezone.activate(link.monitor.timezone) rows = TraceHttp.objects.values_list('time', 'elapsed').filter(time

Django 1.5 Timezone.now()

一个人想着一个人 提交于 2019-12-08 05:26:42
问题 I am newbie and trying to make my Unit Test pass but having problems with DateTimeField. In my settings, I have USE_TZ = True and TIME_ZONE set. Using MongoDb. First the test is giving me an error complaining about comparing offset-naive and offset-aware. Changed auto_now_add=True to datetime.datetime.utcnow().replace(tzinfo=utc)) I Still couldn't get the right time and date to my TIME_ZONE. After I put these in my Database (settings.py) 'OPTIONS' : { 'tz_aware' : True, } Now I can change my

Django: Should I convert an aware datetime instance to UTC before posting to a model?

六眼飞鱼酱① 提交于 2019-12-07 05:42:22
问题 I have aware datetime instances (where tzinfo = "America/Los_Angeles" ) that I would like to save to a model. Should I convert it to UTC somehow before saving? Or can I just save it as is, since it's aware of its own timezone? Do I need to convert it to users' timezones later with activate() , or will Django do that for me, since the instance is aware? I'm most curious what the conventions are in regards to this. Thanks in advance. 回答1: If it is timezone aware and you have USE_TZ set to True

Django 1.5 Timezone.now()

南笙酒味 提交于 2019-12-06 19:31:35
I am newbie and trying to make my Unit Test pass but having problems with DateTimeField. In my settings, I have USE_TZ = True and TIME_ZONE set. Using MongoDb. First the test is giving me an error complaining about comparing offset-naive and offset-aware. Changed auto_now_add=True to datetime.datetime.utcnow().replace(tzinfo=utc)) I Still couldn't get the right time and date to my TIME_ZONE. After I put these in my Database (settings.py) 'OPTIONS' : { 'tz_aware' : True, } Now I can change my TIME_ZONE and the time and date shows my localtime, not utc. But when I run a test model: nf.data

Runtime Warning in django

空扰寡人 提交于 2019-12-05 03:40:38
问题 I am working on a Django project with MySQL as the back-end, this warning keeps troubling me, can anyone please suggest a fix for this. Thanks in advance!! /usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2013-04-22 10:34:44) while time zone support is active. 回答1: You have to make the following changes: In settings.py, you need to add following line: USE_TZ = True Also, in your code you should create

How to format DateTimeField in Django Admin to localtime?

梦想与她 提交于 2019-12-04 21:21:35
问题 How to format DateTimeField in Admin according to localtime and timezone ? My settings.py: TIME_ZONE = 'Europe/Bratislava' LANGUAGE_CODE = 'en-us' USE_I18N = True USE_L10N = True USE_TZ = True pytz package is installed. model: class Material(models.Model): category = models.ForeignKey(Category, null=True, blank=True) code = models.CharField(max_length=10) description = models.CharField(max_length=30, blank=True, null=True) modified = models.DateTimeField(auto_now=True) created = models

Why does time still show with UTC time zone instead of settings.TIME_ZONE?

本小妞迷上赌 提交于 2019-12-04 05:33:37
问题 I have a model that shows a short string in __str__() method of the model def __str__(self): return "Scheduled at %s" % (self.date_time.strftime("%B %d %Y %I:%M %p")) #Output: <Task: Scheduled at September 30 2018 12:30 AM> # it should have been: <Task: Scheduled at September 29 2018 08:30 PM> When I go to the Django admin , I see in the title Task: Scheduled at September 30 2018 12:30 AM and in the input it is filled with the correct TIME_ZONE : 20:30:00 settings.py TIME_ZONE = 'Etc/GMT+4'

Default value of DateTimeField for South migration in Django project with activated timezone support

爷,独闯天下 提交于 2019-12-04 05:11:54
I'm creating a schema migration with South 0.7.6 for my Django 1.4.3 project with enabled timezone support. The schema migration includes adding a DateTimeField (with auto_now=True ) on one table. When creating the migration, South prompts me: The field 'MyTable.my_field' does not have a default specified, yet is NOT NULL. Since you are adding this field, you MUST specify a default value to use for existing rows. Would you like to: 1. Quit now, and add a default to the field in models.py 2. Specify a one-off value to use for existing columns now What's the correct one-off value to give here,

Timezone.now() vs datetime.datetime.now()

只谈情不闲聊 提交于 2019-12-04 01:05:13
When should I be using django's timezone.now() and when should I be using python's datetime.datetime.now() . For example, in the following INSERT which would make more sense? - Product.objects.create(title='Soap', date_added=datetime.datetime.now()) - Product.objects.create(title='Soap', date_added=timezone.now()) Is there a rule of thumb on when to use each? Just always use timezone.now() . Django now has timezone support which requires timezone 'aware' datetime objects. datetime.now() will return a timezone naive object, whereas timezone.now() will return a timezone aware object. You can