0

I'm trying to import csv rows into Django models. The date format in the database I'm using is '%Y/%m/%d %H:%M:%S', and this is the error I get when I try to import the data:

.
.
.
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1406, in get_prep_value
    value = super().get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1266, in get_prep_value
    return self.to_python(value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1388, in to_python
    raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“2013/01/01 07:57:25.689” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']

I have this line my settings.py: DATETIME_FORMAT = '%Y/%m/%d %H:%M:%S'

How do I change the default format to be the same format as my data? Thank you!

Riverendell
  • 23
  • 1
  • 6
  • 1
    `DATETIME_FORMAT` is only concerned with *printing* a `datetime`. – Willem Van Onsem Jul 05 '21 at 15:49
  • Furthermore for models there is only one format, that is the one in the validation error. For a *form* you can specify other formats. – Willem Van Onsem Jul 05 '21 at 15:52
  • @WillemVanOnsem Ah I see, do you have advice how I can import my data then? Ideally I wouldn't want to have to change the format of the data, should I just store it as a CharField? Also, is there really no way to change the default format? – Riverendell Jul 05 '21 at 15:58
  • @AbdulAzizBarkat Sorry I mistyped, it's just a csv I'm trying to import from. Do you have any advice on how I should handle this? – Riverendell Jul 05 '21 at 15:59
  • @AbdulAzizBarkat It does and it works!! I guess I was approaching it from the wrong angle, thank you so much for taking the time to answer! :) – Riverendell Jul 05 '21 at 16:05

1 Answers1

0

As @AbdulAzizBarkat kindly linked, instead of trying to change Django's default datetime string format, I converted the datetime string into a python datetime object first before importing it, as explained in this post: Converting string into datetime.

Riverendell
  • 23
  • 1
  • 6