我正在使用 python 3.9 构建一个 google appengine 应用程序。我使用 cloud ndb 作为我的应用程序的数据库。我的应用程序有一个像这样的项目 ndb 模型:
class item(ndb.Model):
nameFr = ndb.StringProperty()
nameAr = ndb.StringProperty()
nameEn = ndb.StringProperty()
price = ndb.IntegerProperty()
customerBonus = ndb.IntegerProperty()
employeeBounty = ndb.IntegerProperty()
thumbnail = ndb.StringProperty()
category = ndb.KeyProperty(kind='category')
store = ndb.KeyProperty(kind='store')
date = ndb.DateTimeProperty(auto_now_add=True)
但是,当我尝试访问某些属性时(下面的代码)
def item_detail(request,item_id):
if request.method == 'GET':
existingItem = item.get_by_id(int(item_id))
itemStore = store.get_by_id(existingItem.store.id())
itemCategory =category.get_by_id(existingItem.category.id())
context = {'item':existingItem,
'store':itemStore,
'category':itemCategory}
# return render(request,'item/detail-item.html',context)
return HttpResponse(existingItem.date)
我收到错误。 (只能访问价格属性!)
AttributeError at /item/detail/6236280943804416
'item' object has no attribute 'date'
Request Method: GET
Request URL: http://localhost:8000/item/detail/6236280943804416
Django Version: 4.1
Exception Type: AttributeError
Exception Value:
'item' object has no attribute 'date'
Exception Location: /Users/hizan/Desktop/mysite/item/views.py, line 79, in item_detail
Raised during: item.views.item_detail
这是抛出错误时的局部变量:
Local vars
Variable Value
existingItem
item(key=Key('item', 6236280943804416), price=80)
item_id
'6236280943804416'
request
<WSGIRequest: GET '/item/detail/6236280943804416'>
date
是一个很常见的名字。可能有什么东西遮住了它。您可以再试一次,但将 date
定义为 item_date
或更具描述性的内容吗?