I'm learning python and exploring Google App Engine and I have faced this problem:
How to add json array that is within my json request, to ndb object?
Here is my model:
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()
And my code for request handling looks like this:
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()
I've had simpler model before, and i was not using StructuredProperty. My requests were working, but now when i send request like this:
{
"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"
}
I get 500 Server Error
NameError: name 'Vehicle' is not defined
I understand that this might be a very noob question, but I could not find an answer that would've worked for me. Can you please assist me?
Thank you!
I've managed to solve my problem. Thanks to @dragonx for his comment, it helped me a lot.
My Handler:
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()
My model:
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()