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.

Hey,

From the dictionary:

data = {
    'id': 1337,
    'profiles': [
        {'email': 'test@test.com'},
        {'email': 'old_email@test.com'},
    ]
}

Which schema could I write to dump the following result?

{
  "id": 1337,
  "current_email": "test@test.com"
}

Assuming the latest email in the first entry from profiles.

I imagine it should be possible with fields.Pluck, but I can’t make it work.

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
brmzkwcommented, May 20, 2020

Indeed, it is also working. Full working example:

from marshmallow import decorators, fields, Schema

data = {
    'id': 1337,
    'profiles': [
        {'email': 'test@test.com'},
        {'email': 'old_email@test.com'},
    ]
}


class ProfileSchema(Schema):
    email = fields.String()


class UserSchema(Schema):
    id = fields.Int()

    @decorators.post_dump(pass_original=True)
    def add_email(self, data, original_data, many=False):
        data['email'] = original_data['profiles'][0]['email']
        return data


print (UserSchema().dump(data))

pass_original is important here. By default, original data is not given to post_dump.

Thanks!

0reactions
sloriacommented, May 19, 2020

Oh right, custom field isn’t what you want. You could use a post_dump method instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I make a flat list out of a list of lists? - python
To make this more readable, you can make a simple function: def flatten_list(deep_list: list[list[object]]): return list(chain.from_iterable( ...
Read more >
Python: How to Flatten a List of Lists - Stack Abuse
Flattening a list of lists entails converting a 2D list into a 1D list by un-nesting each list item stored in the list...
Read more >
How to flatten list in Python? - TutorialsTeacher
Flatten list of lists means getting elements of sublists into a one dimensional array like list. For example, a list [[1,2,3],[4,5,6]] is ...
Read more >
Python Program to Flatten a Nested List - Programiz
Using itertools module, we can create a flattened list. chain() method from itertools module returns each element of each iterable (i.e. sub lists...
Read more >
How to flatten a list of lists in Python - nkmk note
Flatten list with sum() ... You can also flatten a list of lists with the built-in function sum() . An initial value can...
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