Sean
Sean

Reputation: 545

Marshmallow - Sort field values by declared order

I've read the docs and searched this site but cannot seem to find a solution to sorting field values by the order in which they are declared. The docs state that adding ordered = True to the class Meta will solve this problem -

class MySchema(Schema):
    class Meta:
        ordered = True

However, I am not using class Meta in my schema. My schema simply looks like -

class MySchema(Schema):    
    id = fields.Integer()
    name = fields.Str() 
    category = fields.Str()

So in this situation, how and where would I set ordered = True? Thanks!

Upvotes: 6

Views: 4130

Answers (1)

Sean
Sean

Reputation: 545

I solved the issue by changing my schema class to -

class MySchema(Schema):  
    class Meta:
        ordered = True  

    id = fields.Integer()
    name = fields.Str() 
    category = fields.Str()

and then also adding JSON_SORT_KEYS=False to my app's config.py file.

Upvotes: 11

Related Questions