Comments (3)
Aha, this is tricky case. marshmallow serializes objects by pulling attributes or keys from objects that correspond to the names of a serializer's fields. So, a Serializer
with a nested field named items
will try to pull the items
attribute or key from an object. This is fine for a plain object with an items
attribute.
However, when you try to serialize a dictionary with the same serializer, the serializer will again try to pull the items
attribute from the dictionary, which turns out to be a built-in method of dict
. This is why you are getting a TypeError
saying that you can't iterate over a method.
The short answer to your problem is: Rename the items
field to something else that isn't a built-in attribute or method of dict
.
For example, the following will work:
from marshmallow import Serializer, fields, pprint
class Book(object):
title = ''
author = ''
class BookSerializer(Serializer):
title = fields.String()
author = fields.String()
class BookList(object):
books = list()
class BookListSerializer(Serializer):
books = fields.Nested(BookSerializer, many=True)
book1 = {'title': 'hello android', 'author': 'leejaycoke'}
book2 = {'title': 'hello iOS', 'author': 'tommy'}
book = {'books': [book1, book2]}
pprint(BookListSerializer(book).data)
# {"books": [{"author": "leejaycoke", "title": "hello android"}, {"author": "tommy", "title": "hello iOS"}]}
from marshmallow.
Thank you very much.
I can't understand me why I'd naver tried to change attribute name (items).
from marshmallow.
This answer solves the problem without having to change the key name but it may have a performance cost
#648 (comment)
from marshmallow.
Related Issues (20)
- Wrong value in __call__() function of validate.Validator HOT 1
- Validation of required nested field is not consistent with previous version
- Create raw list Schema HOT 1
- The function decorated by the "post_load" decorator will undergo changes in parameter types as the function name changes. HOT 1
- Deprecate __version__ and related attributes
- Address deprecation warnings in tests HOT 1
- URL validation fails if URL is missing user field HOT 5
- fields.URL validation error should specify "not a valid FQDN" if trying to pass hostname without domain
- missing `f` for f-Strings HOT 2
- `fields.Url` does not accept `file` URLs
- when a new request, schema context not reset !!! HOT 2
- Questions about Copilot + Open Source Software Hierarchy HOT 3
- Allow None for a allow_none=True Date field HOT 6
- fields.Nested Type Warning Since Version 3.15.0
- Setting many=True through Meta - SchemaOpts HOT 1
- RFE: use `zoneinfo` instead of `pytz` HOT 3
- 3.21.2: pytest fails in units checking time HOT 1
- 3.21.2: sphinx warnings `reference target not found` HOT 2
- Static typing of Field subclass kwargs
- Help understanding how to set Schema meta options when using Schema.from_dict() HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from marshmallow.