摘要:嵌套可以嵌套使用以表示對象間的關(guān)系如外鍵關(guān)系。在下面的例子中,和對象是一對多的關(guān)系必須使用或參數(shù)避免無限遞歸也可以使用導(dǎo)入模塊的方式傳遞嵌套,如自嵌套給傳遞字符串參數(shù)表示和對象本身的關(guān)系
schema嵌套
schema可以嵌套使用以表示對象間的關(guān)系(如外鍵關(guān)系)。
例如下例中Blog有一個(gè)用User對象表示的author屬性:
import datetime as dt class User(object): def __init__(self, name, email): self.name = name self.email = email self.created_at = dt.datetime.now() self.friends = [] self.employer = None class Blog(object): def __init__(self, title, author): self.title = title self.author = author # A User object
使用Nested子類接收嵌套的schema表示二者的關(guān)系:
from marshmallow import Schema, fields, pprint class UserSchema(Schema): name = fields.String() email = fields.Email() created_at = fields.DateTime() class BlogSchema(Schema): title = fields.String() author = fields.Nested(UserSchema)
序列化后的blog對象將包含嵌套的user對象:
user = User(name="Monty", email="monty@python.org") blog = Blog(title="Something Completely Different", author=user) result = BlogSchema().dump(blog) pprint(result) # {"title": u"Something Completely Different", # "author": {"name": u"Monty", # "email": u"monty@python.org", # "created_at": "2014-08-17T14:58:57.600623+00:00"}}
如果field嵌套對象是一個(gè)集合,必須設(shè)置many=True,如collaborators = fields.Nested(UserSchema, many=True)
指定嵌套對象的序列化字段設(shè)置only參數(shù)顯式地指定對嵌套對象的哪些屬性進(jìn)行序列化:
class BlogSchema2(Schema): title = fields.String() author = fields.Nested(UserSchema, only=["email"]) schema = BlogSchema2() result = schema.dump(blog) pprint(result) # { # "title": u"Something Completely Different", # "author": {"email": u"monty@python.org"} # }
使用點(diǎn)分隔符可以表示深層嵌套對象的屬性:
class SiteSchema(Schema): blog = fields.Nested(BlogSchema2) schema = SiteSchema(only=["blog.author.email"]) result, errors = schema.dump(site) pprint(result) # { # "blog": { # "author": {"email": u"monty@python.org"} # } # }
如果給only參數(shù)傳遞的是字符串(上面的例子傳遞的是列表),將返回單個(gè)值(上面的例子返回的是鍵值映射)或值的列表(需要設(shè)置many=True):
class UserSchema(Schema): name = fields.String() email = fields.Email() friends = fields.Nested("self", only="name", many=True) # ... create ``user`` ... result, errors = UserSchema().dump(user) pprint(result) # { # "name": "Steve", # "email": "steve@example.com", # "friends": ["Mike", "Joe"] # }雙向嵌套
對于兩個(gè)互相嵌套的對象,可以使用類名引用嵌套的schema,即便是引用時(shí)該schema還沒有被定義。
在下面的例子中,Author和Book對象是一對多的關(guān)系:
class AuthorSchema(Schema): # 必須使用only或exclude參數(shù)避免無限遞歸 books = fields.Nested("BookSchema", many=True, exclude=("author", )) class Meta: fields = ("id", "name", "books") class BookSchema(Schema): author = fields.Nested(AuthorSchema, only=("id", "name")) class Meta: fields = ("id", "title", "author")
from marshmallow import pprint from mymodels import Author, Book author = Author(name="William Faulkner") book = Book(title="As I Lay Dying", author=author) book_result, errors = BookSchema().dump(book) pprint(book_result, indent=2) # { # "id": 124, # "title": "As I Lay Dying", # "author": { # "id": 8, # "name": "William Faulkner" # } # } author_result, errors = AuthorSchema().dump(author) pprint(author_result, indent=2) # { # "id": 8, # "name": "William Faulkner", # "books": [ # { # "id": 124, # "title": "As I Lay Dying" # } # ] # }
也可以使用導(dǎo)入模塊的方式傳遞嵌套schema,如books = fields.Nested("path.to.BookSchema", many=True, exclude=("author", ))
schema自嵌套給Nested傳遞字符串參數(shù)self表示和對象本身的關(guān)系:
class UserSchema(Schema): name = fields.String() email = fields.Email() friends = fields.Nested("self", many=True) # Use the "exclude" argument to avoid infinite recursion employer = fields.Nested("self", exclude=("employer", ), default=None) user = User("Steve", "steve@example.com") user.friends.append(User("Mike", "mike@example.com")) user.friends.append(User("Joe", "joe@example.com")) user.employer = User("Dirk", "dirk@example.com") result = UserSchema().dump(user) pprint(result.data, indent=2) # { # "name": "Steve", # "email": "steve@example.com", # "friends": [ # { # "name": "Mike", # "email": "mike@example.com", # "friends": [], # "employer": null # }, # { # "name": "Joe", # "email": "joe@example.com", # "friends": [], # "employer": null # } # ], # "employer": { # "name": "Dirk", # "email": "dirk@example.com", # "friends": [] # } # }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/43866.html
摘要:方法對應(yīng)的是方法,它反序列化一個(gè)字典為數(shù)據(jù)結(jié)構(gòu)。某些例如和內(nèi)置了驗(yàn)證器驗(yàn)證集合時(shí),錯(cuò)誤字典將基于無效字段的索引作為鍵通過給的參數(shù)傳遞對象,可以執(zhí)行額外的驗(yàn)證驗(yàn)證函數(shù)可以返回布爾值或拋出異常。 快速上手 Declaring Schemas 首先創(chuàng)建一個(gè)基礎(chǔ)的user模型(只是為了演示,并不是真正的模型): import datetime as dt class User(object)...
摘要:創(chuàng)建實(shí)例時(shí)如果傳遞了,表示需要接收輸入數(shù)據(jù)集合,裝飾器注冊預(yù)處理和后處理方法時(shí)需要傳遞參數(shù)。 預(yù)處理和后處理方法 數(shù)據(jù)的預(yù)處理和后處理方法通過pre_load, post_load, pre_dump和post_dump裝飾器注冊: from marshmallow import Schema, fields, pre_load class UserSchema(Schema): ...
摘要:有三種方式創(chuàng)建自定義的。下面的例子判斷某個(gè)對象是否是某個(gè)對象的作者,以及的屬性是否出現(xiàn)單詞自定義錯(cuò)誤信息字段驗(yàn)證產(chǎn)生的錯(cuò)誤信息可以在類級別或?qū)嵗墑e配置。在類級別時(shí),可以定義為錯(cuò)誤碼和錯(cuò)誤信息的字典映射在類實(shí)例化時(shí),給參數(shù)傳參對象 有三種方式創(chuàng)建自定義的field。 創(chuàng)建Field類的子類 創(chuàng)建繼承自marshmallow.fields.Field類的子類并實(shí)現(xiàn)_serialize和/...
摘要:下面我們來說說如何使用來減輕序列化模型的工作量。主要包括如下個(gè)步驟定義模式序列化模型下面我們分別來看看。不得不說這個(gè)庫對于序列化模型其實(shí)挺實(shí)用的。 原文地址: http://52sox.com/use-python-serialization-orm-data-to-json/ 相信使用Python做Web開發(fā)的朋友都會(huì)遇到這樣1個(gè)問題,那就是在項(xiàng)目開發(fā)中使用模型框架,比如SQLAlc...
摘要:是以太坊開發(fā)的個(gè)人區(qū)塊鏈,可用于部署合約,開發(fā)應(yīng)用程序和運(yùn)行測試。安裝是一個(gè)用于與以太坊交互的庫。啟動(dòng)以太坊測試區(qū)塊鏈服務(wù)器要部署智能合約,我們應(yīng)該啟動(dòng)測試以太坊服務(wù)器。最后,你將在以太坊合約中設(shè)置調(diào)用用戶對象時(shí)獲得的值。 將數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫中是任何軟件應(yīng)用程序不可或缺的一部分。無論如何控制該數(shù)據(jù)庫都有一個(gè)該數(shù)據(jù)的主控。區(qū)塊鏈技術(shù)將數(shù)據(jù)存儲(chǔ)到區(qū)塊鏈網(wǎng)絡(luò)內(nèi)的區(qū)塊中。因此,只要某個(gè)節(jié)點(diǎn)與網(wǎng)...
閱讀 2279·2021-11-22 09:34
閱讀 1413·2021-10-11 10:59
閱讀 4528·2021-09-22 15:56
閱讀 3434·2021-09-22 15:08
閱讀 3472·2019-08-30 14:01
閱讀 854·2019-08-30 11:16
閱讀 1183·2019-08-26 13:51
閱讀 2971·2019-08-26 13:43