marshmallow

Python marshmallow tree structure with polymorphism

寵の児 提交于 2020-07-16 06:52:33
问题 I have the following code for a tree structure: class Node: def __init__(self, node_id: str): self.node_id = node_id self.children = [] def add_child(self, node: 'Node'): if isinstance(node, Node): self.children.append(node) class ValueNode(Node): def __init__(self, value: bool, **kwargs): Node.__init__(self, **kwargs) self.value = value class LogicNode(Node): def __init__(self, logic_operator: str, **kwargs): Node.__init__(self, **kwargs) self.logic_operator = logic_operator a = Node("a") b

Use DB data model to generate SQLAlchemy models, schemas, and JSON response

谁都会走 提交于 2020-04-12 21:45:32
问题 Using Flask and SQLAlchemy for a Python webapp, my goal is to create a system in which I can: Import data models from an existing PostgreSQL DB, and map these to fields in corresponding SQLAlchemy models Use these SQLAlchemy models to automatically generate a schema. This schema will then be used to perform data validation on user-submitted data.(I'm currently trying to use Marshmallow, but am open to other suggestions). Perform JSON response formatting using the schema generated in step 2.

Modeling relationships in SQLAlchemy to show nested “sets” with Marshmallow

依然范特西╮ 提交于 2020-02-07 05:30:27
问题 I'm trying to figure out how to model (flask-)SQLAlchemy and (flask-)Marshamallow to provide the following JSON output. The parent is a product and the children are "variant sets"... which are types of options, like "color" or "size". Nested within those sets I want the options themselves (S, M, L, etc.) Seems like I'm missing something obvious. Desired output: { "skuid": "B1234", "name": "Test Product 1", "variant_sets": [ { "variant_set": "B1234_1", "variants": [ { "code": "S", "variant

Marshmallow result customization

大憨熊 提交于 2020-01-06 11:17:28
问题 I have the sqlalchemy model with jsonb field and marshmallow schema for this model: class Settings(db.Model): id = db.Column(UUID, primary_key=True, server_default=text("uuid_generate_v4()")) settings = db.Column(JSONB) class SettingsSchema(ma.ModelSchema): class Meta: model = Settings I'm serializing it in json like: settings = Settings(settings={'qwerty': 'test'}) settings_schema = SettingsSchema(many=False, exclude=('id', )) body = settings_schema.dump(settings).data and json view of the

Why is “Object of type Decimal is not JSON serializable” - when using marshmallow with SQLAlchemy automap?

社会主义新天地 提交于 2020-01-03 01:40:08
问题 Using automap_base from sqlalchemy.ext.automap to map my tables. Not able to shema.dumps(result) ; getting raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Decimal is not JSON serializable Tried using JSON custom decoders, but no use. from flask import Flask from flask_sqlalchemy import SQLAlchemy from sqlalchemy.orm import Session from sqlalchemy.ext.automap import automap_base from flask_marshmallow import Marshmallow app = Flask(__name__) app.config[

Python (flask/ marshmallow)ValueError: too many values to unpack (expected 2)

痴心易碎 提交于 2020-01-02 16:23:11
问题 I am working on a Flask project and I am using marshmallow to validate user input. Below is a code snippet: def create_user(): in_data = request.get_json() data, errors = Userschema.load(in_data) if errors: return (errors), 400 fname = data.get('fname') lname = data.get('lname') email = data.get('email') password = data.get('password') cpass = data.get('cpass') When I eliminate the errors part, the code works perfectly. When I run it as it is, I get the following error: builtins.ValueError

Is it possible to validate list using marshmallow?

巧了我就是萌 提交于 2020-01-01 08:25:09
问题 Is it possible to validate list using marshmallow? class SimpleListInput(Schema): items = fields.List(fields.String(), required=True) # expected invalid type error data, errors = SimpleListInput().load({'some': 'value'}) # should be ok data, errors = SimpleListInput().load(['some', 'value']) Or it is expected to validate only objects? 回答1: To validate top-level lists, you need to instantiate your list item schema with many=True argument. Example: class UserSchema(marshmallow.Schema): name =

Is it possible to validate list using marshmallow?

浪子不回头ぞ 提交于 2020-01-01 08:25:08
问题 Is it possible to validate list using marshmallow? class SimpleListInput(Schema): items = fields.List(fields.String(), required=True) # expected invalid type error data, errors = SimpleListInput().load({'some': 'value'}) # should be ok data, errors = SimpleListInput().load(['some', 'value']) Or it is expected to validate only objects? 回答1: To validate top-level lists, you need to instantiate your list item schema with many=True argument. Example: class UserSchema(marshmallow.Schema): name =

Flask Marshmallow/SqlAlchemy: Serializing many-to-many relationships

点点圈 提交于 2019-12-30 04:45:07
问题 I'm building a small REST api using Flask, flask-sqlalchemy and flask-marshmallow. For some requests I'd like to return a json serialized response consisting of my sqlalchemy objects. However I cant get the serialization to work with eagerly loaded sqlalchemy objects when using many-to-many relationships / secondary tables. Here is a simple example, more or less copy/pasted from flask-marshmallow docs: from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_marshmallow

How to nest some parent object fields when serializing objects using marshmallow

前提是你 提交于 2019-12-23 22:16:00
问题 I'm using marshmallow to serialize a sqlalchemy object that has a flat structure like: class Company(Base): __tablename__ = 'Company' id = sa.Column(sa.Integer, primary_key=True) MainStreetAddress = sa.Column(sa.String) MainCity = sa.Column(sa.String) MainState = sa.Column(sa.String) MainZip = sa.Column(sa.String) AltStreetAddress = sa.Column(sa.String) AltCity = sa.Column(sa.String) AltState = sa.Column(sa.String) AltZip2 = sa.Column(sa.String) When serializing, I would like marshmallow to