2017-02-10 5 views
0

私はpythonを学び、Google App Engineを探索しています。この問題に直面しました:json配列をリクエストから取得し、GAEを使用してPythonを使用してオブジェクトで使用する方法はありますか?

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":"[email protected]", 
    "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" 
} 

私は

NameError: name 'Vehicle' is not defined

500サーバーエラーを取得する私は、これは非常にnoobの質問かもしれませんが、私はできなかったことを理解し私のために働いていた答えを見つけてください。私を助けてもらえますか?

ありがとうございました!

+1

クラスドライバは、クラスの車両に言及しているように見えますが、車はまだ定義されていません。 Vehicle定義を上に移動すると、このエラーが発生します。 – dragonx

答えて

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() 
関連する問題