LKM LKM - 6 months ago 32
Python Question

Django ) AttributeError : 'datetime.date' object when trying to get an object of model

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


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

Thank you!

Answer

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.