Serializing a list of objects with django-rest-framework

前端 未结 1 1445
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 09:39

In DRF, I can serialize a native Python object like this:

class Comment(object):
    def __init__(self, email, content, created=None):
        self.email = e         


        
相关标签:
1条回答
  • 2021-01-01 10:09

    You can simply add many=True for serialising list.

    comments = [Comment(email='leila@example.com', content='foo bar'),
                Comment(email='leila1@example.com', content='foo bar 1'),
                Comment(email='leila2@example.com', content='foo bar 2')]
    serializer = CommentSerializer(comments, many=True)
    serializer.data
    
    0 讨论(0)
提交回复
热议问题