棉花糖的婚嫁与诠释
marshmallow-annotations的Python项目详细描述
marshmallow注释允许您从类创建marshmallow架构 上面有注释
frommarshmallow_annotationsimportAnnotationSchemafrom.musicimportAlbum,ArtistclassAlbumScheme(AnnotationSchema):classMeta:target=Albumregister_as_scheme=TrueclassArtistScheme(AnnotationSchema):classMeta:target=Artistregister_as_scheme=Truescheme=ArtistScheme()scheme.dump(Artist(id=1,name="Abominable Putridity",albums=[Album(id=1,name="The Anomalies of Artificial Origin")]))# {# "albums": [# {# "id": 1,# "name": "The Anomalies of Artificial Origin"# }# ],# "id": 1,# "name": "Abominable Putridity"# }
安装
棉花糖注释在pypi上可用 可通过以下方式安装:
pip install marshmallow-annotations
marshmallow注解支持python 3.6+和marshmallow 2.x.x
注意
如果要在虚拟机外部安装marshmallow-annotations。 环境,请考虑安装 pip install --usermarshmallow-annotations而不是使用sudo或 管理员权限,以避免将其安装到系统python中。
为什么?
与实体定义、orm映射和模式形状保持一致可能是一个巨大的 痛屁股。如果你改变一件事,你需要改变三件事。
相反,棉花糖注释希望从 实体定义(当然需要你的帮助)。