Django AttributeError 'datetime.date' object has no attribute 'utcoffset'
Asked Answered
K

4

13

I'm a newbie in Django, so sorry if explanation of problem looks weird.

I created a blog app within a Django project.

models.py:

from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=255)
    pub_date = models.DateTimeField() # issue appears because of this statement
    body = models.TextField()
    image = models.ImageField(upload_to='images/')

When I create a new blog object in admin page of site, everything's fine and object gets created and stored to the database. But when I want to edit this blog object (clicking on it in the list of blogs), I get this error:

AttributeError at /admin/blog/blog/2/change/
'datetime.date' object has no attribute 'utcoffset'

Full traceback:

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/blog/blog/2/change/

Django Version: 2.0.2
Python Version: 3.6.4
Installed Applications:
['jobs.apps.JobsConfig',
 'blog.apps.BlogConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Users\LENOVO\Anaconda3\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19
   'datetime.date' object has no attribute 'utcoffset'
   9 :             {% for field in line %}
   10 :                 <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
   11 :                     {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
   12 :                     {% if field.is_checkbox %}
   13 :                         {{ field.field }}{{ field.label_tag }}
   14 :                     {% else %}
   15 :                         {{ field.label_tag }}
   16 :                         {% if field.is_readonly %}
   17 :                             <div class="readonly">{{ field.contents }}</div>
   18 :                         {% else %}
   19 :                              {{ field.field }} 
   20 :                         {% endif %}
   21 :                     {% endif %}
   22 :                     {% if field.field.help_text %}
   23 :                         <div class="help">{{ field.field.help_text|safe }}</div>
   24 :                     {% endif %}
   25 :                 </div>
   26 :             {% endfor %}
   27 :         </div>
   28 :     {% endfor %}
   29 : </fieldset>


Traceback:

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  156.                 response = response.render()

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  175.                     return self._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
  211.                     nodelist.append(node.render_annotated(context))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
  194.                 return template.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  177.                 return self._render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
  211.                     nodelist.append(node.render_annotated(context))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
  211.                     nodelist.append(node.render_annotated(context))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
  314.                 return nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\defaulttags.py" in render
  314.                 return nodelist.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render
  999.         return render_value_in_context(output, context)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\template\base.py" in render_value_in_context
  978.             value = str(value)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\utils\html.py" in <lambda>
  371.     klass.__str__ = lambda self: mark_safe(klass_str(self))

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\boundfield.py" in __str__
  36.         return self.as_widget()

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\boundfield.py" in as_widget
  118.             **kwargs

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\widgets.py" in render
  234.         context = self.get_context(name, value, attrs)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\contrib\admin\widgets.py" in get_context
  104.         context = super().get_context(name, value, attrs)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\widgets.py" in get_context
  806.             value = self.decompress(value)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\widgets.py" in decompress
  894.             value = to_current_timezone(value)

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\forms\utils.py" in to_current_timezone
  176.     if settings.USE_TZ and value is not None and timezone.is_aware(value):

File "C:\Users\LENOVO\Anaconda3\lib\site-packages\django\utils\timezone.py" in is_aware
  247.     return value.utcoffset() is not None

Exception Type: AttributeError at /admin/blog/blog/2/change/
Exception Value: 'datetime.date' object has no attribute 'utcoffset'

If I change models.DateTimeField() to models.DateField() or if I add auto_now_add=True to DateTimeField (so that it makes this field uneditable) everything becomes ok and blog object becomes editable without errors.

So the question obviously is what caused this error and how to fix it?

Django version 2.0.2 (also tried on 2.0.5)

Python version 3.6.4

Katabolism answered 16/8, 2018 at 5:22 Comment(4)
models.DateTimeField(auto_now_add=True) or models.DateTimeField(auto_now=True) or give the value to DateTimeField manually while creating instance or modelTris
@Hemanth SP, when I created an instance of model, I manually selected date and time of course. This doesn't work, still if I want to edit already created instance, I get AttributeError.Katabolism
add blank=True, null=True to edit already created instance,Tris
@Hemanth SP, nope, still same error, I tried just nowKatabolism
A
24

Seems like model instance returns you date data. Take a look on database column and see if its type is not date. Probably previously during database creation your model had pub_date as DateField. If so, run makemigrations and migrate to adjust database type according your current model.

Accounting answered 16/8, 2018 at 5:42 Comment(1)
Your guess was right, I initially had this filed as DateField. Applying migrations indeed helped. Many thanks, man!Katabolism
D
1

It may because you have changed or alter some of your models fields and you didn't migrate your project to update your database. all you need to do run migrate command and you are good to go.

Diamagnet answered 27/6, 2019 at 16:9 Comment(0)
P
0

Note: Documenting my answer eventhough it's not directly related to the question, however for the same exception:

Django AttributeError 'datetime.date' object has no attribute 'utcoffset'

I got the same error when I used django drf serializers.DateTimeField for a model field which is defined as models.DateField

class Invoice(models.Model):
    # ... other fields
    due_date = models.DateField()
from rest_framework import serializers


class InvoiceSerializer(serializers.ModelSerializer)
    due_date = serializers.DateTimeField()

    class Meta:
        model = Invoice

Changing it to due_date = serializers.DateField() fixed the issue.

Platino answered 13/3, 2024 at 9:23 Comment(0)
D
0

In my case I was passing a date object in a function expecting datetime.

from django.contrib.humanize.templatetags import humanize
today=timezone.now().date()
next_date = humanize.naturaltime(today)
Deserted answered 22/6, 2024 at 20:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.