4

I need to update a model object and save so that this result can be reflected.

But when I try to get an object of this model that I checked already this object exists,

modelOne = lkmModel.objects.get(id=1)

I got an error :

AttributeError: 'datetime.date' object has no attribute 'tzinfo'

I searched already , yes I have a field about 'date' but this is field is :

matchingDate = models.DateTimeField(default=datetime.date.today())

As I searched, my field 'matchingDate' is not a problem, isn't it?

I'm going to write my models.py below :

from django.db import models
import datetime

class lkmModel(models.Model):
    id = models.IntegerField()
    matchingDate = models.DateTimeField(default=datetime.date.today())
    isNoon = models.BooleanField(default=True)
    dayOfWeek = models.IntegerField()
    profileUrl = models.TextField()
    name = models.CharField(max_length=20,default=None)
    affiliation = models.CharField(max_length=20,default=None)
    age = models.IntegerField(db_index=True)
    residence = models.CharField(max_length=20,default=None)
    bloodType = models.CharField(max_length=20,default=None)
    religion = models.CharField(max_length=20,default=None)

    personality = models.TextField(default=None)
    appearance = models.TextField(default=None)
    hobby = models.TextField(default=None)
    ability = models.TextField(default=None)
    idealType = models.TextField(default=None)
    career = models.TextField(default=None)
    best = models.TextField(default=None)

    badge1 = models.CharField(max_length=10,default=None)
    badge2 = models.CharField(max_length=10,default=None)
    badge3 = models.CharField(max_length=10,default=None)

    manual = models.TextField(default=None)
    point = models.IntegerField()

    def __str__(self):
        return  self.name

Even though I changed 'matchingDate' field to :

matchingDate = models.DateTimeField()

When I queried , exactly the same error is occurring, I think it is more serious than I expected...

UPDATE! It's the image of the screen after querying 'matchingDate' columnn enter image description here

Is there anything I did wrong? My python version is 3.4.4 and django's version is 1.9.2

Thank you!

0

2 Answers 2

2

First, your model cannot have a field named id without it being declared as a primary_key. Secondly, while creating django models use django.utils.timezone.now instead of datetime.

The root cause of the problem you have is that you're giving datetime.date object as default to a datetime.datetime instance. Hence it says that tzinfo attribute i.e. time zone information is missing. tzinfo is an attribute to datetime and time objects, not date objects. You can read more about it here: https://docs.python.org/2/library/datetime.html

So, after modifications your models.py should look like this:

from django.db import models
from django.utils import timezone

class lkmModel(models.Model):
    id = models.IntegerField(primary_key=True)
    matchingDate = models.DateTimeField(default=timezone.now)
    isNoon = models.BooleanField(default=True)
    dayOfWeek = models.IntegerField()
    profileUrl = models.TextField()
    name = models.CharField(max_length=20,default=None)
    affiliation = models.CharField(max_length=20,default=None)
    age = models.IntegerField(db_index=True)
    residence = models.CharField(max_length=20,default=None)
    bloodType = models.CharField(max_length=20,default=None)
    religion = models.CharField(max_length=20,default=None)

    personality = models.TextField(default=None)
    appearance = models.TextField(default=None)
    hobby = models.TextField(default=None)
    ability = models.TextField(default=None)
    idealType = models.TextField(default=None)
    career = models.TextField(default=None)
    best = models.TextField(default=None)

    badge1 = models.CharField(max_length=10,default=None)
    badge2 = models.CharField(max_length=10,default=None)
    badge3 = models.CharField(max_length=10,default=None)

    manual = models.TextField(default=None)
    point = models.IntegerField()

    def __str__(self):
        return  self.name

This should solve the problem.

Sign up to request clarification or add additional context in comments.

12 Comments

OK, then can you explain why these points you pointed affected the error 'Attribute error : datetime.date' doesn't have a field tzinfo'?
@LKM I've edited my answer to include the root cause of your problem. Please take a look.
But it returns the same error after changing just what you said. anyway thank you for fighting for me
Did you migrated the changes and then re-instantiated the database? If you're trying to get that old object it would lead to an error.
I did 'python manage.py makemigrations' and then 'python manage.py migrate' and I did test and I failed, is there something I'm missing now?
|
2

The first thing is do not use id in models. Django adds that automatically as the primary key.

The other thing is that you are trying to fill date object in a Datetime field. Datetime is different from Date.

matchingDate = models.DateTimeField(default=datetime.date.today())

Instead use

matchingDate = models.DateTimeField(default=datetime.now())

Better still, use auto_now_add if you want to use the default time as the time of creation of the object.(Sorry if i am misunderstanding the purpose)

matchingDate = models.DateTimeField(auto_now_add=True)

But the actual error comes from timezone info thing.

Install pytz (pip install pytz)

and try to use the datetime with time zone info (tzinfo) instead of a naive one.

time = datetime.now(pytz.utc)

This gives timezone enabled datetime

datetime.date has no attribute tzinfo. You have to use it on datetime object only.

1 Comment

Sorry, it doesn't work as what you said. thank you anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.