question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ordered = True not respected

See original GitHub issue
class BaseSchema(Schema):
     id = fields.Int(dump_only=True)

class LineItemSchema(BaseSchema):
    name = fields.Str(attribute='label')

class CampaignSchema(BaseSchema):
    name = fields.Str(attribute='campaign_name')
    line_items = fields.Nested(LineItemSchema, many=True)

    class Meta:
        fields = ("id", "name", "line_items")
        ordered = True

When I run: result = campaign_schema.dump(campaign) json.dumps(result.data)

I get this output:

{
    "id": 1, 
    "line_items": [
        {
            "id": 1, 
            "name": "mpu"
        }, 
        {
            "id": 3, 
            "name": "banner"
        }
    ], 
    "name": "test"
}

I had a look in my debugger - think it’s something to do with that some of the field names are the same? Perhaps something to do with how ret is constructed here: https://github.com/marshmallow-code/marshmallow/blob/dev/marshmallow/schema.py#L729

Let me know if I’m missing something in my config somewhere!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
azzamsacommented, Jun 26, 2020

This also happens in flask-smorest 0.22.0. All you need to do is disable JSON_SORT_KEYS in your config, as @halfar-aspire mentioned.

app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False
4reactions
billmccordcommented, Apr 7, 2016

This has something to do with json.dumps. I traced it into that method and it is not respecting the OrderedDict at least in Python 3.5.1. If you are using Flask-Restful, this could be why: http://flask-restful-cn.readthedocs.org/en/latest/extending.html “Note If the application is running in debug mode (app.debug = True) and either sort_keys or indent are not declared in the RESTFUL_JSON configuration setting, Flask-RESTful will provide defaults of True and 4 respectively.”

Read more comments on GitHub >

github_iconTop Results From Across the Web

ordered = True not respected. python + marshmallow + Flask
looks like property ordered=True on ChildSchema class is not working I have been looking for that in some posts and looks like the...
Read more >
Marshmallow not respecting ordered=True
I had a little fight with Marshmallow and the ordered=True in class Meta. When ordered=True is set the fields should be outputed in...
Read more >
3.3.3: Reaction Order - Chemistry LibreTexts
This can be found by adding the reaction orders with respect to the reactants. ... The order of a reaction is not necessarily...
Read more >
Pandas Sort: Your Guide to Sorting Data in Python - Real Python
To sort the DataFrame based on the values in a single column, you'll use .sort_values() . By default, this will return a new...
Read more >
Column ordering and sorting not working on reload - DataTables
My DT code is below. $('table.tasks-index').dataTable({ "processing": true, "responsive": true, "lengthChange": true, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found