"DateTimeField %s received a naive datetime (%s) - python

'2022-11-11'
this is the input value getting from the front end,
RuntimeWarning: DateTimeField PaymentChart.date received a naive datetime (2022-11-18 00:00:00) while time zone support is active.
this is the error that coming
paydate = datetime.datetime.strptime(date,'%Y-%m-%d').isoformat()
this is how i tried to convert the date, and not working,
i got this error before, and i added 'tz=datetime.timezone.utc' , it was workin fine then
offer.expiry=datetime.datetime.now(tz=datetime.timezone.utc)+datetime.timedelta(days=28)
but how can i add tz in strptime ??

You have to use Django's datetime, and not "datetime" library's datetime:
from django.utils import timezone
import pytz
offer.expiry=timezone.now()(tzinfo=pytz.UTC)+datetime.timedelta(days=28, tzinfo=pytz.UTC)

Related

How to Convert a Date to Django Timezone Aware Datetime

I am trying to convert a DateTime with the UTC time zone to Django datetime that respects the time zone of my date.
Here is my settings.py:
TIME_ZONE = 'US/Pacific'
USE_TZ = True
Here is the date I am trying to convert to the right time zone for django: datetime.datetime(2020, 2, 20, 8, 55, 48, 846000)
I used the make_aware function provided by Django but I can see the date isn't converted to the time zone in my settings.py when I save the Django Modal with a DateTime field
from django.utils.timezone import make_aware
Django doc
When time zone support is enabled (USE_TZ=True), Django uses time-zone-aware datetime objects. If your code creates datetime objects, they should be aware too. In this mode, the example above becomes:

django postgres datetime field to python datetime

I have the following model class
class Transaction(models.Model):
quantity = models.IntegerField(default=0)
sell_time = models.DateTimeField()
When I fetch "sell_time" from the model, I am getting datetime in the following format
2014-10-01 08:09:46.251563+00:00
my question is, if it is not in the format like
year-month-day hour:minutes:seconds
how can I convert to python datetime object like
datetime.datetime(2014, 10, 1, 08, 09, 46, 540535)
many thanks
Did you print it? If you print a datetime object, it is serialized to a string but it is a datetime. You can use it as any other datetime.
You are seeing that because you did not set the correct time zone. Use the UTC time and then you can format .strftime("format") the time accordingly to each locale, you can install pytz and enable it in the settings to handle all the hustle. Always use UTC datetime object because then you can get what ever time you want knowing the zone your user is in. Documentation from Django

How to remove/convert naive time to time zone aware in Django

I am trying to read a date of format DD-MM-YYYY format from HTML and Compare it with the datetime field named as widget_created_at in my model.
if data["field"] == "widget_created_at":
date = datetime.strptime(data["data"], "%d-%m-%Y").date()
if data["option"] == "before":
filter_query = Q(widget_created_at__lt=date)
Then whenever this query is used for filtering in the below code
blogs = blogs.filter(filter_query)
RunTime warning for naive datetime appears. I tried every solution i found from here and google but the Error is still there. Please tell me how to avoid it.
Thanks
The date you are creating for your filter has no timezone.
Have you looked at pytz to "localize" the filter date? This would allow you to add a timezone to the filter date?

Compare date and datetime in Django

I have a model with a datetime field:
class MyModel(models.Model):
created = models.DateTimeField(auto_now = True)
I want to get all the records created today.
I tried:
MyModel.objects.all().filter(created = timezone.now())
and
MyModel.objects.all().filter(created = timezone.now().date())
But always got an empty set. What is the correct way in Django to do this?
EDIT:
It looks strange, but a record, created today (06.04.2012 23:09:44) has date (2012-04-07 04:09:44) in the database. When I'm trying to edit it in the admin panel it looks correct (06.04.2012 23:09:44). Does Django handle it somehow?
Since somewhere in 2015:
YourModel.objects.filter(some_datetime__date=some_date)
i.e. __date after the datetime field.
https://code.djangoproject.com/ticket/9596
There may be a more proper solution, but a quick workup suggests that this would work:
from datetime import timedelta
start_date = timezone.now().date()
end_date = start_date + timedelta( days=1 )
Entry.objects.filter(created__range=(start_date, end_date))
I'm assuming timezone is a datetime-like object.
The important thing is that you're storing an exact time, down to the millisecond, and you're comparing it to something that only has accuracy to the day. Rather than toss the hours, minutes, and seconds, django/python defaults them to 0. So if your record is createed at 2011-4-6T06:34:14am, then it compares 2011-4-6T:06:34:14am to 2011-4-6T00:00:00, not 2011-4-6 (from created date) to 2011-4-6 ( from timezone.now().date() ). Helpful?
Try this
from datetime import datetime
now=datetime.now()
YourModel.objects.filter(datetime_published=datetime(now.year, now.month, now.day))

Python/Django: Have DB TimeZoneField for user, how do I use that with datetime and time to get strftime() like '%Y-%m-%dT%H:%M:%S.000Z'?

I have a DB "TimeZoneField" type for users, how do I use that with the "datetime.datetime()" object to get a strftime() string like '%Y-%m-%dT%H:%M:%S.000Z'?
This was for Google Calendar's Python data API feed and it was a lot of work because Python's datetime library doesn't support ISO 8601:
http://wiki.python.org/moin/WorkingWithTime
In addition if you transmit dates with .000Z timezone to Google calendar it will ignore DST (Daylight Savings Time) for events that occur in EDT and others (so things will be off by an hour for parts of the year.)
Here is my fix: Assuming start_time and end_time are timezone aware datetime.datetime objects:
timezone_string = start_datetime.strftime('%z')[0:3] + ":" + start_datetime.strftime('%z')[3:6]
start_time = start_datetime.strftime('%Y-%m-%dT%H:%M:%S' + timezone_string)
end_time = end_datetime.strftime('%Y-%m-%dT%H:%M:%S' + timezone_string)
Note that stftime("%z") doesn't include that ":" character to separate the hours/minutes in the offset that Google's calendar API requires.

Categories

Resources