如何从请求中获取 json 数组并在对象中使用 python 和 GAE?
How to get json array from request and use it in object using python with GAE?
我正在学习 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: name 'Vehicle' is not defined
我知道这可能是一个非常菜鸟的问题,但我找不到适合我的答案。你能帮帮我吗?
谢谢!
我已经设法解决了我的问题。感谢@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()
我正在学习 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: name 'Vehicle' is not defined
我知道这可能是一个非常菜鸟的问题,但我找不到适合我的答案。你能帮帮我吗?
谢谢!
我已经设法解决了我的问题。感谢@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()