我正在尝试在GAE上的ndb中创建1对多的关系。
当我尝试将客户端密钥分配给Customer.client_id属性时,将抛出此错误:
*** BadValueError:预期列表或元组,获得Key('Client',5629499534213120)
import sys, pprint
pprint.pprint(sys.path)
from flask import Flask
from flask import Flask, render_template, request, session
app = Flask(__name__)
app.secret_key = 'superSecretKey'
from google.appengine.ext import ndb
class Client(ndb.Model):
email = ndb.StringProperty()
name = ndb.StringProperty(indexed=True)
signup = ndb.DateTimeProperty(auto_now_add=True)
class Customer(ndb.Model):
client_id = ndb.KeyProperty(kind=Client, repeated=True)
email = ndb.StringProperty()
name = ndb.StringProperty(indexed=True)
signup = ndb.DateTimeProperty(auto_now_add=True)
# this just creates a Client to use
if not ( Client.query( Client.name == "Bryan Wheelock").get()):
client = Client(
email = "bryan@email.com",
name = "Bryan Wheelock",
).put()
@app.route('/')
def main_page():
client = Client.query( Client.name == "Bryan Wheelock").get()
session['client'] = client.key.urlsafe()
return render_template('index.html',
client=client
)
@app.route('/submitted', methods=['POST'])
def submitted_form():
client = ndb.Key(urlsafe=session['client']).get()
print "###########################################"
print("client.key = " + str(client.key))
customer = Customer()
customer.client_id = client.key
customer.name = request.form.get('id_name')
customer.put()
return render_template('submitted_form.html',
client=client,
customer=customer
)
@app.errorhandler(500)
def server_error(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred.', 500
ERROR 2017-01-25 16:10:39,337 main.py:82] An error occurred during a request.
Traceback (most recent call last):
File "/Users/bryanwheelock/work/flask_TDD/lib/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Users/bryanwheelock/work/flask_TDD/lib/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/bryanwheelock/work/flask_TDD/lib/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/bryanwheelock/work/flask_TDD/lib/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/bryanwheelock/work/flask_TDD/lib/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/bryanwheelock/work/flask_TDD/main.py", line 65, in submitted_form
customer.client_id = client.key
File "/Users/bryanwheelock/work/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1400, in __set__
self._set_value(entity, value)
File "/Users/bryanwheelock/work/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1141, in _set_value
(value,))
BadValueError: Expected list or tuple, got Key('Client', 5629499534213120)
答案 0 :(得分:2)
在您的客户模型实体中
client_id = ndb.KeyProperty(kind=Client, repeated=True)
重复= True表示您将拥有一个Keys列表而不是一个键。所以你要么需要删除重复= True,要么需要像:
customer.client_id = [client.key]
或者它不是新实体
customer.client_id.append(client.key)