Django AttributeError: 'str' object has no attribute 'tzinfo'

11,357

There is an issue with your data from the DateTimeField field. The data needs to be in common DateTime format.

From the docu: https://docs.djangoproject.com/en/3.0/ref/forms/fields/#datetimefield

Normalizes to: A Python datetime.datetime object.

Validates that the given value is either a datetime.datetime, \
datetime.date or string formatted in a particular datetime format.

Your data has to match one of the below:

[
    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M:%S.%f',  # '2006-10-25 14:30:59.000200'
    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59'
    '%m/%d/%Y %H:%M:%S.%f',  # '10/25/2006 14:30:59.000200'
    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59'
    '%m/%d/%y %H:%M:%S.%f',  # '10/25/06 14:30:59.000200'
    '%m/%d/%y %H:%M',        # '10/25/06 14:30'
    '%m/%d/%y',              # '10/25/06'
]
Share:
11,357

Related videos on Youtube

Noootron27
Author by

Noootron27

Updated on June 04, 2022

Comments

  • Noootron27
    Noootron27 almost 2 years

    I am getting this error at the following code, when I try to get all the objects from my database:

    data1 = Data.objects.all()
    for dataset in data1:
    

    Here is my model:

    class Data(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
    path = models.TextField(db_column='Path')  # Field name made lowercase.
    username = models.ForeignKey('Users', models.DO_NOTHING, db_column='Username')  # Field name made lowercase.
    datatype = models.CharField(db_column='Datatype', max_length=20, blank=True, null=True)  # Field name made lowercase.
    filesize = models.FloatField(db_column='Filesize', blank=True, null=True)  # Field name made lowercase.
    creationdate = models.DateTimeField(db_column='CreationDate')  # Field name made lowercase.
    modificationdate = models.DateTimeField(db_column='ModificationDate')  # Field name made lowercase.
    diskname = models.CharField(db_column='Diskname', max_length=100, blank=True, null=True)  # Field name made lowercase.
    
    class Meta:
        managed = False
        db_table = 'Data'
    

    The full error message is:

        Internal Server Error: /files/
    Traceback (most recent call last):
      File "/home/pi/.local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
        response = get_response(request)
      File "/home/pi/.local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "/home/pi/.local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/home/pi/.local/lib/python3.7/site-packages/django/views/decorators/cache.py", line 31, in _cache_controlled
        response = viewfunc(request, *args, **kw)
      File "/home/pi/.local/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
        return view_func(request, *args, **kwargs)
      File "/home/pi/MakMakula/FileManager/app/views.py", line 57, in index
        for dataset in data1:
      File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/query.py", line 276, in __iter__
        self._fetch_all()
      File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
        self._result_cache = list(self._iterable_class(self))
      File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/query.py", line 74, in __iter__
        for row in compiler.results_iter(results):
      File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1095, in apply_converters
        value = converter(value, expression, connection)
      File "/home/pi/.local/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 265, in convert_datetimefield_value
        value = timezone.make_aware(value, self.connection.timezone)
      File "/home/pi/.local/lib/python3.7/site-packages/django/utils/timezone.py", line 270, in make_aware
        return timezone.localize(value, is_dst=is_dst)
      File "/home/pi/.local/lib/python3.7/site-packages/pytz/__init__.py", line 237, in localize
        if dt.tzinfo is not None:
    AttributeError: 'str' object has no attribute 'tzinfo'
    [02/Jun/2020 10:43:44] "GET /files/ HTTP/1.1" 500 110853
    

    Does anybody have any ideas why this comes up?

    • Edison Feneyab
      Edison Feneyab almost 4 years
      How did you insert the data? Especially for creationdate and modificationdate?
    • Noootron27
      Noootron27 almost 4 years
      In general the data is inserted by another python application, however for testing purposes I entered it by hand, it should be correct...One set of data looks like this: 2020-06-02 12:30:19
  • Noootron27
    Noootron27 almost 4 years
    In general the data is inserted by another python application, however for testing purposes I entered it by hand, it should be correct...One set of data looks like this: 2020-06-02 12:30:19