Questions tagged [marshmallow]

Marshmallow is an ORM/ODM/framework-agnostic library for converting complex datatypes, such as objects, to and from native Python datatypes. (For Questions about Android Marshmallow use the tag [android-6.0-marshmallow].)

Marshmallow is an ORM/ODM/framework-agnostic library for converting complex datatypes, such as objects, to and from native Python datatypes. See https://marshmallow.readthedocs.io/en/latest/index.html

589 questions
30
votes
6 answers

Update row (SQLAlchemy) with data from marshmallow

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): #…
gcerar
  • 920
  • 1
  • 13
  • 24
25
votes
4 answers

Is it possible to validate list using marshmallow?

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,…
avasin
  • 9,186
  • 18
  • 80
  • 127
23
votes
4 answers

How to serialize a Marshmallow field under a different name

I want a Marshmallow Schema with the following output json - { "_id": "aae216334c3611e78a3e06148752fd79", "_time": 20.79606056213379, "more_data" : {...} } Marshmallow doesn't serialize private members so this is as close as I can get -…
selotape
  • 846
  • 1
  • 13
  • 26
20
votes
1 answer

What are the best practices for combining marshmallow schema definitions and OO in Python?

Assume a simple schema defined in marshmallow class AddressSchema(Schema): street=fields.String(required=True) city=fields.String(required=True) country=fields.String(default='USA') class PersonSchema(Schema): …
Chris Mungall
  • 629
  • 5
  • 13
18
votes
11 answers

'Marshmallow' object has no attribute 'ModelSchema'

Everything looks fine from the documentation but it still gives me this error when I'm running the app: File "main.py", line 21, in class UserSchema(ma.ModelSchema): AttributeError: 'Marshmallow' object has no attribute…
Art Chaz
  • 421
  • 1
  • 3
  • 11
15
votes
2 answers

How should I add a field containing a list of dictionaries in Marshmallow Python?

In Marshmallow in order to have a list field you can use: include_in = fields.List(cls_or_instance=fields.Str(), default=['sample1', 'sample2']) This is OK, but I have a new requirement to have a list of dictionaries in a…
Alireza
  • 6,497
  • 13
  • 59
  • 132
15
votes
5 answers

how to serialise a enum property in sqlalchemy using marshmallow

this is my model class class Type(enum.Enum): Certified = "certified" Non_Certified = "non-certified" class Status(enum.Enum): Approved = "Approved" Rejected = "Rejected" Published = "Published" Retired = "Retired" …
kartikey
  • 358
  • 1
  • 3
  • 13
14
votes
1 answer

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

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…
MSurrow
  • 1,183
  • 2
  • 9
  • 23
13
votes
3 answers

Using Marshmallow without repeating myself

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 =…
Teyras
  • 1,262
  • 11
  • 22
13
votes
3 answers

How to dynamically generate marshmallow schemas for SQLAlchemy models

I'm creating a Flask API using SQLAlchemy models. I don't want to define a schema for every model I have, I don't want to do this every time: class EntrySchema(ma.ModelSchema): class Meta: model = Entry I would like each model to have a…
Jasper
  • 363
  • 2
  • 10
13
votes
3 answers

handling optional field validation

I have a simple problem and am unsure the best way to handle it. I have a schema defined as follows: class MySchema(Schema): title = fields.String(required=True) imageUrl = fields.Url() imageUrl is an optional field, sometimes it will be…
lostdorje
  • 6,150
  • 9
  • 44
  • 86
12
votes
2 answers

How to handle nested relationships in marshmallow-sqlalchemy

I have a project where I'm trying to load data from JSON into a Sqlalchemy database using the marshmallow-sqlalchemy package. The model contains a one-to-many relationship with a child model. Using the classic example of an author with many…
Anthony Oteri
  • 402
  • 4
  • 14
12
votes
4 answers

Deserialize nested fields in marshmallow

I'm consuming an API that returns something like: {'name': 'foo', 'start': {'date': '2016-06-19', 'time': '18:00'}} And I want to desearialize it with marshmallow to get only the name and the start date, so the desired result would be the…
camaya
  • 377
  • 1
  • 4
  • 15
12
votes
3 answers

Short way to serialize datetime with marshmallow

Here is my situation: I store some datetime in MSSQL, which i get in my python application via SQLAlchemy, and then serialize it thru Marshmallow like this: class MyVisitSchema(Schema): cafe = fields.Nested(CafeSchema) started_at =…
Alexey Lysenko
  • 1,890
  • 2
  • 12
  • 28
12
votes
4 answers

How can I serialize a MongoDB ObjectId with Marshmallow?

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…
1
2 3
39 40