marshmallow-code / marshmallow-code/marshmallow
How to extract key from schema to place at a different place in response?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
Hi,
I need to duplicate a nested value in a response and hike it up to the root of the response JSON, but I am completely lost as to how to accomplish this.
I would like test to return the target_value in the response json
Thank you for all your help!
**Current Schemas in question:**
```
class ProposalItemResponseSchema(BaseSchema):
test = fields.Method("get_test")
def get_test(self, data):
return self.context['test_mode']
class ProposalItemFullResponseSchema(ProposalItemResponseSchema):
package = fields.Nested(PackageResponseSchema)
screens = fields.Nested(ScreenFullResponseSchema, many=True)
end_date = fields.Date(default=None)
active_type = EnumField(TypeOfBuy)
combined_end_time = fields.DateTime(default=None)
combined_start_time = fields.DateTime(default=None)
provider_id = fields.Integer(default=None)
class Meta:
# Need this to be backward compatible with Marshmallow v2.x
# https://marshmallow.readthedocs.io/en/stable/upgrading.html#datetime-leaves-timezone-information-untouched-during-serialization
datetimeformat = "%Y-%m-%dT%H:%M:%S+00:00"
def get_attribute(self, obj, attr, default):
# Save the proposal item id in the context for retrieving the proper
# proposal_item_screen.contribution in the screen nested schema
if attr == "screens":
self.context["proposal_item_screens"] = {pis.screen_id: pis for pis in obj.proposal_item_screens}
self.context["proposal_item"] = obj
return getattr(obj, attr, default)
class ProposalFullResponseSchema(ProposalSchema):
id = fields.Integer()
calculated_start_date = fields.Str(default=None, data_key="start_date")
calculated_end_date = fields.Str(default=None) # Returned as a string from the model.
proposal_items = fields.Nested(ProposalItemFullResponseSchema, many=True)
categories = fields.Nested(CategoryResponseSchema, many=True)
class Meta:
# Need this to be backward compatible with Marshmallow v2.x
# https://marshmallow.readthedocs.io/en/stable/upgrading.html#datetime-leaves-timezone-information-untouched-during-serialization
datetimeformat = "%Y-%m-%dT%H:%M:%S+00:00"
def get_attribute(self, obj, attr, default):
# Save the proposal item id in the context for retrieving the proper
# proposal_item_screen.contribution in the screen nested schema
self.context["test_mode"] = obj
return getattr(obj, attr, default)
```
**Response JSON**
```
{
"proposal_items": [
{
"test": {
"target_value": 983,
"fluff 1": 983,
"fluff 2": 983
}
}
]
}
```
**Desired Response**
```
{
"proposal_items": [
{
"test": "target_value"
}
]
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading ProposalItemResponseSchema, ProposalItemFullResponseSchema, and ProposalFullResponseSchema, then compare the current and desired response JSON. Determine whether the requested nested-value relocation is supported by the schema definitions and identify the relevant Marshmallow field or schema behavior; done means documenting or demonstrating a clear way to produce the desired response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100