1

I want to store DateTimeField data from csv to my model field but I am getting error

RuntimeWarning: DateTimeField Applications.submitted_at received a naive datetime (2020-01-30 11:08:20.429620) while time zone support is active. RuntimeWarning)

my models.py

 submitted_at = models.DateTimeField(default=datetime.now, blank=True)
 form_fill_up_datetime = models.DateTimeField(auto_now=True, auto_now_add=True),

how can I resolve this issue?

here is my function to convert csv string data into datetimefield

def date_check(data):#
    if data is None:
        print("Date Field is empty")
        return
    try:
        try:
            date_format1 = '%m/%d/%Y %H:%M:%S'
            date_obj = datetime.datetime.strptime(data, date_format1)
            return date_obj
        except ValueError:
            print('Incorrect data format, should be YYYY-MM-DD')
            date_format2 = '%Y-%m-%d %H:%M:%S'
            date_obj = datetime.datetime.strptime(data, date_format2)
            return date_obj
    except Exception as e:
        print(e)
        return

1 Answers1

1

What you see is not an error, but a warning. It says that it is odd that you are running the server timezone-aware, and then you still pass a datetime object without a timezone. You can add a timezone, like UTC to your datetime object:

from pytz import utc

def date_check(data):#
    if data is None:
        print("Date Field is empty")
        return
    try:
        try:
            date_format1 = '%m/%d/%Y %H:%M:%S'
            date_obj = datetime.datetime.strptime(data, date_format1)
            return utc.localize(date_obj)
        except ValueError:
            print('Incorrect data format, should be YYYY-MM-DD')
            date_format2 = '%Y-%m-%d %H:%M:%S'
            date_obj = datetime.datetime.strptime(data, date_format2)
            return utc.localize(date_obj)
    except Exception as e:
        print(e)
        return
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555