“Key”对象没有属性“database”Google Cloud Ndb 中的错误

问题描述 投票:0回答:1

我正在使用 python 3.9 运行 google appengine 应用程序。我使用 cloud ndb 作为我的应用程序的数据库。 我的应用程序有一个客户 ndb 模型。创建实体时一切正常。但是,当我想更新实体时,应用程序会抛出此错误。

这是我添加新客户的代码

newCustomer = customer( number = number,
               addedBy = ndb.Key('employee',int(employeeId)),
               firstName = firstname,
               lastName = lastname,
               phone = phone,
               countryCode= countryCode)
order_key = newCustomer.put()

这是我的更新代码

client = ndb.Key('customer',customer_id).get()
customer.firstName = firstName
customer.lastName = lastName
customer.phone = phone
customer.countryCode = countryCode
customer_key = customer.put()

这是我遇到的错误

AttributeError at /customer/edit/6199943289110528
'Key' object has no attribute 'database'
Request Method: POST
Request URL:    http://localhost:8000/customer/edit/6199943289110528
Django Version: 4.1
Exception Type: AttributeError
Exception Value:    
'Key' object has no attribute 'database
python google-cloud-platform google-app-engine
1个回答
0
投票

您需要更改此代码:

client = ndb.Key('customer',customer_id).get()
customer.firstName = firstName
customer.lastName = lastName
customer.phone = phone
customer.countryCode = countryCode
customer_key = customer.put()

对于这样的事情:

existingCustomer = ndb.Key('customer',customer_id).get()
existingCustomer.firstName = firstName
existingCustomer.lastName = lastName
existingCustomer.phone = phone
existingCustomer.countryCode = countryCode
customer_key = existingCustomer.put()

customer
是ndb模型类的名称,您需要将其与使用该类创建的实例/对象区分开来。

© www.soinside.com 2019 - 2024. All rights reserved.