如何从请求中获取json数组,并在带有GAE的python中将其用于对象?

费多尔·齐加诺夫(Fedor Tsyganov)

我正在学习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":"[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"
}

我收到500伺服器错误

NameError:未定义名称“车辆”

我知道这可能是一个非常菜鸟般的问题,但是我找不到适合我的答案。你能帮我吗?

谢谢!

费多尔·齐加诺夫(Fedor Tsyganov)

我设法解决了我的问题。感谢@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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从带有Angular的json对象的数组中获取特定集合

来自分类Dev

如何从带有对象的嵌套数组中获取值

来自分类Dev

React Native:如何将自定义函数应用于对象数组,并在Javascript中返回带有附加字段的修改后的数组?

来自分类Dev

带有对象无法获取的json数组

来自分类Dev

如何显示带有索引的数组。我正在从 HTTP GET 请求中获取数组

来自分类Dev

如何导航 JSON 文件,获取所需的对象并使用 javascript 将其推送到数组中?

来自分类Dev

如何从响应中获取数据数组(带有数组的对象)?

来自分类Dev

如何检查字符串并在python中获取带有特殊字符的单词

来自分类Dev

对JSON对象求和并在Python中获取JSON文件

来自分类Dev

如何从excel列中获取所有值并将其放入python中的数组中?

来自分类Dev

无法从 JSON 字符串中获取 Java 对象(带有内部数组)

来自分类Dev

如何从文件中获取图像,并在同一程序中将其覆盖?

来自分类Dev

如何从cloud_firestore获取数据并在flutter中将其显示在TextField中

来自分类Dev

如何获取JSON数组中对象的索引?

来自分类Dev

如何从js数组中获取json对象

来自分类Dev

如何返回带有json数组的json对象?

来自分类Dev

javascript-如何获取文本框的值并将其放入带有jquery的(key:Value)数组中?

来自分类Dev

如何调用带有对象和数组的函数并将其添加到该对象的属性?

来自分类Dev

Spring MVC:发布请求和带有数组的json对象:错误请求

来自分类Dev

如何使用Python发送带有数据的获取请求?

来自分类Dev

如何从文件中删除重复的行并在Python中将其写入文件

来自分类Dev

在javascript中获取JSON对象的对象名称并将其存储在字符串数组中

来自分类Dev

如何在重复控件中访问带有 json 对象的数组列表

来自分类Dev

从配置文件中读取Json对象列表并在控制器中将其返回始终返回null

来自分类Dev

带有json的java中的对象返回数组

来自分类Dev

在php中创建JSON对象(带有数组)

来自分类Dev

PHP返回带有JSON数组中特殊字符的对象

来自分类Dev

如何从REstAssured中的Json数组中获取JSON对象

来自分类Dev

如何通过带有请求模块的Python在json中打印Twitter处理?

Related 相关文章

  1. 1

    如何从带有Angular的json对象的数组中获取特定集合

  2. 2

    如何从带有对象的嵌套数组中获取值

  3. 3

    React Native:如何将自定义函数应用于对象数组,并在Javascript中返回带有附加字段的修改后的数组?

  4. 4

    带有对象无法获取的json数组

  5. 5

    如何显示带有索引的数组。我正在从 HTTP GET 请求中获取数组

  6. 6

    如何导航 JSON 文件,获取所需的对象并使用 javascript 将其推送到数组中?

  7. 7

    如何从响应中获取数据数组(带有数组的对象)?

  8. 8

    如何检查字符串并在python中获取带有特殊字符的单词

  9. 9

    对JSON对象求和并在Python中获取JSON文件

  10. 10

    如何从excel列中获取所有值并将其放入python中的数组中?

  11. 11

    无法从 JSON 字符串中获取 Java 对象(带有内部数组)

  12. 12

    如何从文件中获取图像,并在同一程序中将其覆盖?

  13. 13

    如何从cloud_firestore获取数据并在flutter中将其显示在TextField中

  14. 14

    如何获取JSON数组中对象的索引?

  15. 15

    如何从js数组中获取json对象

  16. 16

    如何返回带有json数组的json对象?

  17. 17

    javascript-如何获取文本框的值并将其放入带有jquery的(key:Value)数组中?

  18. 18

    如何调用带有对象和数组的函数并将其添加到该对象的属性?

  19. 19

    Spring MVC:发布请求和带有数组的json对象:错误请求

  20. 20

    如何使用Python发送带有数据的获取请求?

  21. 21

    如何从文件中删除重复的行并在Python中将其写入文件

  22. 22

    在javascript中获取JSON对象的对象名称并将其存储在字符串数组中

  23. 23

    如何在重复控件中访问带有 json 对象的数组列表

  24. 24

    从配置文件中读取Json对象列表并在控制器中将其返回始终返回null

  25. 25

    带有json的java中的对象返回数组

  26. 26

    在php中创建JSON对象(带有数组)

  27. 27

    PHP返回带有JSON数组中特殊字符的对象

  28. 28

    如何从REstAssured中的Json数组中获取JSON对象

  29. 29

    如何通过带有请求模块的Python在json中打印Twitter处理?

热门标签

归档