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.

Serialize multiple attributes using Pluck

See original GitHub issue

Can Pluck be used to deserialize multiple source attributes to a single field and then serialize back to multiple fields? I read in #1315 that the intent of Pluck is to go from flat -> nested -> flat, but I couldn’t figure out how to get from the input:

{'baz': 'blue', 'qux': 'orange'}

by deserializing to

{'bar': {'baz': 'blue', 'qux': 'orange'}}

and then serializing back to the above.

I could get eitherbaz or qux but not both.

I attempted something like

class Bar(Schema):
    baz = fields.String()
    qux = fields.String()


class Foo(Schema):
    bar_1 = fields.Pluck(Bar, 'baz', data_key='baz', attribute='bar')
    bar_2 = fields.Pluck(Bar, 'qux', data_key='qux', attribute='bar')

But this throws the error:

ValueError: The attribute argument for one or more fields collides with another field's name or attribute argument. Check the following field names and attribute arguments: ['bar']

Is there something I’m missing, or is this use case not supported by Pluck?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
erdnaxelicommented, Aug 12, 2022

@plondino to solve your issue, you could do this:

class Foo(Schema):
    bar_1 = fields.String(attribute='bar.baz', data_key='baz')
    bar_2 = fields.String(attribute='bar.qux', data_key='qux')

Foo().load({'baz': 'blue', 'qux': 'orange'})  # => {'bar': {'baz': 'blue', 'qux': 'orange'}}
Foo().dumps({'bar': {'baz': 'blue', 'qux': 'orange'}})  # => {"baz": "blue", "qux": "orange"}

It makes me a bit wonder why the Pluck field type exists in the first place, but it just works 🤷

1reaction
plondinocommented, Jun 11, 2021

Thanks for the analysis, very helpful. I was thinking that I could do the flattening and nesting in a post_dump and pre_load, respectively, but was getting a little tripped up in whether the philosophy is to have the schema match the input/serialized data or the internalized representation (and not sure it’s explicit in the docs).

If the latter is the case, the above approach seems like good syntax for what is probably a common use case.

I also have nested schemas (i.e. two or more levels down) that I’d like to be able to flatten on serialization; fields.Nested supports the dot notation as an argument to only but it doesn’t flatten, it only excludes. I wonder if the above syntax could support dotted attributes as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pluck with multiple columns? - laravel - Stack Overflow
So first, you GET all the values from DB that you will need, after that you can use any accessor attribute defined for...
Read more >
How to pluck multiple attributes using eloquent collections in ...
In this quick example I will show you how to pluck multiple columns from a collection using map instead of pluck .
Read more >
Nesting Schemas — marshmallow 3.19.0 documentation
You can replace nested data with a single value (or flat list of values if many=True ) using the Pluck field. class UserSchema(Schema):...
Read more >
Add a hash pluck method? - Google Groups
I know this can (almost) be achieved with Student.select(:firstname).map(&:attributes) (it forces you to have an id attribute), but this involves ...
Read more >
Running a delete statement - Orator ORM
These attributes are then assigned to the model via mass-assignment. ... We can define a many-to-many relation using the belongs_to_many decorator:.
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