python-Django:为什么会出现此错误:AttributeError:'method_descriptor'对象没有属性'today'? [英] python - django: why am I getting this error: AttributeError: 'method_descriptor' object has no attribute 'today'?
本文介绍了python-Django:为什么会出现此错误:AttributeError:'method_descriptor'对象没有属性'today'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下python代码:
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()
在python shell中,我试图运行:
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'对象没有属性'today'
推荐答案
您可能想要导入datetime,而不是从datetime导入datetime。
You probably want "import datetime", not "from datetime import datetime".
date是datetime模块上的一个类,但它是一个也是 datetime.datetime类上的方法。
"date" is a class on the datetime module, but it is also a method on the "datetime.datetime" class.
这篇关于python-Django:为什么会出现此错误:AttributeError:'method_descriptor'对象没有属性'today'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文