python - djangjo:为什么我得到这个错误:AttributeError:'method_descriptor'对象没有属性'今天'? [英] python - djangjo: why am i getting this error: AttributeError: 'method_descriptor' object has no attribute 'today'?
本文介绍了python - djangjo:为什么我得到这个错误:AttributeError:'method_descriptor'对象没有属性'今天'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下python代码:
from django.db import models
from datetime import datetime
class Poll(models.Model):
question = models.CharField(max_length = 200)
pub_date = models.DateTimeField('date published')
def __unicode __ self)
return self.question
def was_published_today(self):
return self.pub_date.date()== datetime.date.today()
在python shell中,我试图运行:
p = Poll.objects.get(pk = 1)
p.was_published_today()
第一行工作正常,但第二行给我这个错误:
AttributeError:'method_descriptor'对象没有属性'today'
解决方案
您可能需要导入datetime,而不是from datetime import datetime。 b
$ b
date是datetime上的一个类模块,但它也是datetime.datetime类的一个方法。
I have the following python code:
from django.db import models
from datetime import datetime
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
In a python shell, I am trying to run:
p = Poll.objects.get(pk=1)
p.was_published_today()
The first line works fine but the second line gives me this error:
AttributeError: 'method_descriptor' object has no attribute 'today'
解决方案
You probably want "import datetime", not "from datetime import datetime".
"date" is a class on the datetime module, but it is also a method on the "datetime.datetime" class.
这篇关于python - djangjo:为什么我得到这个错误:AttributeError:'method_descriptor'对象没有属性'今天'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文