Coder Social home page Coder Social logo

Comments (3)

sloria avatar sloria commented on August 19, 2024 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.

leejaycoke avatar leejaycoke commented on August 19, 2024

Thank you very much.
I can't understand me why I'd naver tried to change attribute name (items).

from marshmallow.

charalamm avatar charalamm commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.