我正在学习python并探索Google App Engine,我遇到了这个问题:
如何将我的json请求中的json数组添加到ndb对象?
这是我的模特:
class Driver (ndb.Model):
id = ndb.StringProperty()
first_name = ndb.StringProperty()
last_name = ndb.StringProperty()
date_of_birth = ndb.StringProperty()
phone_number = ndb.StringProperty()
email = ndb.StringProperty()
vehicles = ndb.StructuredProperty(Vehicle, repeated=False)
document = ndb.StructuredProperty(Document, repeated=False)
device_registration_id = ndb.StringProperty()
time_created = ndb.TimeProperty()
time_updated = ndb.TimeProperty()
account_status = ndb.StringProperty()
driver_status = ndb.StringProperty()
class Vehicle (ndb.Model):
car_make = ndb.StringProperty()
car_model = ndb.StringProperty()
car_year = ndb.StringProperty()
license_plate_number = ndb.StringProperty()
class Document (ndb.Model):
driver_license = ndb.StringProperty()
insurance_provider = ndb.StringProperty()
insurance_id = ndb.StringProperty()
insurance_expiration_date = ndb.StringProperty()
我的请求处理代码如下所示:
class DriverManagementNew(webapp2.RequestHandler):
def post(self):
jsonstring = self.request.body
jsonobject = json.loads(jsonstring)
driver_id = str(uuid.uuid4())
new_driver = Driver(
id=driver_id,
first_name=jsonobject["first_name"],
last_name=jsonobject["last_name"],
date_of_birth=jsonobject["date_of_birth"],
phone_number=jsonobject["phone_number"],
email=jsonobject["email"],
vehicles=Vehicle(car_make=jsonobject["car_make"],
car_model=jsonobject["car_model"],
car_year=jsonobject["car_year"],
license_plate_number=jsonobject["license_plate_number"]),
document=Document(driver_license=jsonobject["driver_license"],
insurance_provider=jsonobject["insurance_provider"],
insurance_id=jsonobject["insurance_id"],
insurance_expiration_date=jsonobject["insurance_expiration_date"]),
device_registration_id=jsonobject["device_registration_id"],
time_created=datetime.datetime.now(),
time_updated=datetime.datetime.now(),
account_status=jsonobject["account_status"],
driver_status=jsonobject["driver_status"])
new_driver.put()
之前我的模型比较简单,而且我没有使用StructuredProperty。我的请求正在运行,但现在当我发送这样的请求时:
{
"first_name":"FName",
"last_name":"LName",
"date_of_birth":"01-02-1900",
"phone_number":"+1123123123",
"email":"test@test.com",
"vehicles":{"car_make":"volkswagen",
"car_model":"jetta",
"car_year":"2000",
"license_plate_number":"ABC01DC"
},
"document":{"driver_license":"F3377232G",
"insurance_provider":"Geico",
"insurance_id":"1433123aa",
"insurance_expiration_date":"02-02-2018"
},
"device_registration_id":"id123123123123123",
"account_status":"ACTIVATED",
"driver_status":"ACTIVE"
}
我得到 500服务器错误
NameError:名称'Vehicle'未定义
我知道这可能是一个非常noob的问题,但我找不到一个对我有用的答案。你能帮帮我吗?
谢谢!
答案 0 :(得分:0)
我设法解决了我的问题。感谢@dragonx的评论,这对我帮助很大。
我的处理程序:
class DriverManagementNew(webapp2.RequestHandler):
def post(self):
jsonstring = self.request.body
jsonobject = json.loads(jsonstring)
driver_id = str(uuid.uuid4())
vehicle = Vehicle(car_make=jsonobject["vehicles"]["car_make"],
car_model=jsonobject["vehicles"]["car_model"],
car_year=jsonobject["vehicles"]["car_year"],
license_plate_number=jsonobject["vehicles"]["license_plate_number"])
doc = Document(driver_license=jsonobject["document"]["driver_license"],
insurance_provider=jsonobject["document"]["insurance_provider"],
insurance_id=jsonobject["document"]["insurance_id"],
insurance_expiration_date=jsonobject["document"]["insurance_expiration_date"])
new_driver = Driver(
id=driver_id,
first_name=jsonobject["first_name"],
last_name=jsonobject["last_name"],
date_of_birth=jsonobject["date_of_birth"],
phone_number=jsonobject["phone_number"],
email=jsonobject["email"],
vehicles=vehicle,
document=doc,
device_registration_id=jsonobject["device_registration_id"],
time_created=datetime.datetime.now(),
time_updated=datetime.datetime.now(),
account_status=jsonobject["account_status"],
driver_status=jsonobject["driver_status"])
new_driver.put()
我的模特:
class Vehicle (ndb.Model):
car_make = ndb.StringProperty()
car_model = ndb.StringProperty()
car_year = ndb.StringProperty()
license_plate_number = ndb.StringProperty()
class Document (ndb.Model):
driver_license = ndb.StringProperty()
insurance_provider = ndb.StringProperty()
insurance_id = ndb.StringProperty()
insurance_expiration_date = ndb.StringProperty()
class Driver (ndb.Model):
id = ndb.StringProperty()
first_name = ndb.StringProperty()
last_name = ndb.StringProperty()
date_of_birth = ndb.StringProperty()
phone_number = ndb.StringProperty()
email = ndb.StringProperty()
vehicles = ndb.StructuredProperty(Vehicle, repeated=False)
document = ndb.StructuredProperty(Document, repeated=False)
device_registration_id = ndb.StringProperty()
time_created = ndb.DateTimeProperty()
time_updated = ndb.DateTimeProperty()
account_status = ndb.StringProperty()
driver_status = ndb.StringProperty()