Serialize multiple attributes using Pluck
See original GitHub issueCan 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:
- Created 2 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@plondino to solve your issue, you could do this:
It makes me a bit wonder why the
Pluck
field type exists in the first place, but it just works 🤷Thanks for the analysis, very helpful. I was thinking that I could do the flattening and nesting in a
post_dump
andpre_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 toonly
but it doesn’t flatten, it only excludes. I wonder if the above syntax could support dotted attributes as well.