marshmallow

Using Marshmallow without repeating myself

筅森魡賤 提交于 2019-12-22 03:31:31
问题 According to the official Marshmallow docs, it's recommended to declare a Schema and then have a separate class that receives loaded data, like this: class UserSchema(Schema): name = fields.Str() email = fields.Email() created_at = fields.DateTime() @post_load def make_user(self, data): return User(**data) However, my User class would look something like this: class User: def __init__(name, email, created_at): self.name = name self.email = email self.created_at = created_at This seems like

Using Marshmallow without repeating myself

不羁的心 提交于 2019-12-22 03:31:09
问题 According to the official Marshmallow docs, it's recommended to declare a Schema and then have a separate class that receives loaded data, like this: class UserSchema(Schema): name = fields.Str() email = fields.Email() created_at = fields.DateTime() @post_load def make_user(self, data): return User(**data) However, my User class would look something like this: class User: def __init__(name, email, created_at): self.name = name self.email = email self.created_at = created_at This seems like

Update row (SQLAlchemy) with data from marshmallow

十年热恋 提交于 2019-12-20 11:49:35
问题 I'm using Flask, Flask-SQLAlchemy, Flask-Marshmallow + marshmallow-sqlalchemy, trying to implement REST api PUT method. I haven't found any tutorial using SQLA and Marshmallow implementing update. Here is the code: class NodeSchema(ma.Schema): # ... class NodeAPI(MethodView): decorators = [login_required, ] model = Node def get_queryset(self): if g.user.is_admin: return self.model.query return self.model.query.filter(self.model.owner == g.user) def put(self, node_id): json_data = request.get

SQLAlchemy @property causes 'Unknown Field' error in Marshmallow with dump_only

痞子三分冷 提交于 2019-12-20 04:25:22
问题 I'm using flask-marshmallow (marshmallow=v3.0.0rc1, flask-marshmallow=0.9.0) and flask-sqlalchemy (sqlalchemy=1.2.16, flask-sqlalchemy=2.3.2) I have this model and schema. from marshmallow import post_load, fields from .datastore import sqla_database as db from .extensions import marshmallow as ma class Study(db.Model): _id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) tests = db.relationship("Test", backref="study", lazy='select', cascade="all, delete

Automatic dictionary key resolution with nested schemas using Marshmallow

安稳与你 提交于 2019-12-14 03:45:36
问题 I have a Marshmallow schema where an objects use a key to refer to an object that is defined in a dictionary in another part of the structure. I want to have the key automatically resolved when deserializing the object. How can I achieve this effect in Marshmallow in an idiomatic manner? The workaround for now is to resolve all of the references manually, but this seems clunky, since the declarative nature of Marshmallow should be able to do it for us automatically. Note that Marshmallow

PATCHing resources with nested objects with Flask/SQLAlchemy

感情迁移 提交于 2019-12-12 20:45:50
问题 I have the following setup: # models class Author(BaseModel): id = Column(Integer, primary_key=True) first_name = Column(String(64)) last_name = Column(String(64)) class Book(db.Model): id = Column(Integer, primary_key=True) title = Column(String(64)) author_id = Column(Integer, ForeignKey("author.id"), nullable=True) author = relationship(Author, backref=backref('books')) # schema class AuthorSchema(BaseSchema): first_name = fields.Str() last_name = fields.Str() class Meta(BaseSchema.Meta):

How can I serialize a MongoDB ObjectId with Marshmallow?

喜你入骨 提交于 2019-12-12 07:59:15
问题 I'm building and API on top of Flask using marshmallow and mongoengine. When I make a call and an ID is supposed to be serialized I receive the following error: TypeError: ObjectId('54c117322053049ba3ef31f3') is not JSON serializable I saw some ways with other libraries to override the way the ObjectId is treated. I haven't figured it out with Marshmallow yet, does anyone know how to do that? My model is: class Process(db.Document): name = db.StringField(max_length=255, required=True, unique

Python Marshmallow - Customize formatting of class member

[亡魂溺海] 提交于 2019-12-12 04:54:24
问题 I have Marshmallow serializer written as class TestSchema(Schema): created_date = fields.Str(required=False) class Test(database.Model): created_date = database.Column(database.DateTime, default=datetime.utcnow, nullable=False) testSchema = TestSchema() testSchema.dump(new Test()) Is there any way I can change the output of created_date using created_date.isoformat() ? 回答1: Use fields.DateTime marshmallow field for SQLAlchemy datetime object instead of fields.Str . In fact, iso format is

marshmallow: convert dict to tuple

拜拜、爱过 提交于 2019-12-12 02:44:42
问题 Given that the input data x are: {'comm_name': 'XXX', 'comm_value': '1234:5678', 'dev_name': 'router-1'} The marshmallow schema is as follows: class BGPcommunitiesPostgresqlSchema(marshmallow.Schema): comm_name = marshmallow.fields.Str(required=True) comm_value = marshmallow.fields.Str(required=True) @marshmallow.validates('comm_value') def check_comm_value(self, value): if value.count(":") < 1: raise marshmallow.ValidationError("a BGP community value should contain at least once the `:` char

Flatten data on Marshallow / SQLAlchemy Schema

大城市里の小女人 提交于 2019-12-11 09:39:53
问题 I'm having some issues with how I'm getting my data back from one of my endpoints - specifically, with Marshmallow and SQLAlchemy. I have a many-to-many relationship between cocktails and ingredients, but I also have more data than just foreign keys on the relational table, ings_in_cocktail, such as ounces. When I GET /cocktails/ , it returns something like this: { "cocktails": [ { "glass": "rocks", "ingredients": [ { "ingredient": { "ing_type": "liquor", "id": 1, "name": "gin" }, "ounces":