2017-01-25 4 views
1

Я пытаюсь создать отношение 1 к многим в ndb на GAE.Почему я получаю BadValueError, когда я назначаю ключ для ndb.KeyProperty?

При попытке присвоить ключ клиента к Customer.client_id собственности эта ошибка возникает:

*** BadValueError: ожидаемый список или кортеж, получил ключ ('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 = "[email protected]", 
    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) 

ответ

2

в вашей модели Customer лица у вас есть

client_id = ndb.KeyProperty(kind=Client, repeated=True) 

Непрекращающиеся = True означает, что вы собираетесь получить список ключей, а не только одной клавиши. Таким образом, вам необходимо либо удалить повторен = True или вам нужно иметь что-то вроде:

customer.client_id = [client.key] 

или, если это не новый объект

customer.client_id.append(client.key) 
Смежные вопросы