Define fields programmatically in Marshmallow Schema

后端 未结 4 542
不思量自难忘°
不思量自难忘° 2021-02-09 05:27

Say I have a Schema like this:

class MySchema(Schema):

    field_1 = Float()
    field_2 = Float()
    ...
    field_42 = Float()

Is there a w

4条回答
  •  自闭症患者
    2021-02-09 05:47

    The class Meta paradigm allows you to specify which attributes you want to serialize. Marshmallow will choose an appropriate field type based on the attribute’s type.

    class MySchema(Schema):
        class Meta:
            fields = ('field_1', 'field_2', ..., 'field_42')
        ...
    

    Refactoring: Implicit Field Creation

提交回复
热议问题