-
-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fields.Nested should optionally pass the parent object to the Nested Schema #940
Comments
I'm not opposed to the idea. Feel free to send a PR for review. However, I can't promise we'll get to it in the immediate future because we're currently focused on the 3.0 final release. |
I don't think this should be a concern of the serialization layer really. Maybe a fix for the problem described in #900 is to add a computed property:
|
Related SO question: https://stackoverflow.com/questions/50485096/. I proposed @timc13's solution in an answer. Shortcomings:
|
@timc13 That is a totally valid workaround and addresses my case. But I still think this feature should be added, and here is why:
|
Could this be addressed by adding a class MySchema(Schema):
nested = fields.Nested()
...
@pre_dump
def prepare_nested(item)
item['nested'] = item
return item This way, the field does not have to access parent object. See #1046. |
+1 - I could really use this feature. The legacy structure I'm adapting can have nested fields at multiple levels, and it'd be really great to understand which one a field belongs to. |
@lafrech Isolating fields from each other is a great idea, but is an implementation detail. |
Hi, To work around this, we did something like this: def identity_accessor(attr, obj, default):
return obj or default
class Section(fields.Nested):
def get_value(self, attr, obj, accessor=None, default=missing):
return super(Section, self).get_value(attr, obj,
accessor=identity_accessor,
default=default) Each Section field receives the full object. |
Best way to solve this problem is to set the "attribute" kwarg on the Nested field itself to a callable that returns the root object:
|
I also solved this using a (simpler) variant of @m-a-choquette's method: class SelfNested(fields.Nested):
def get_value(self, obj, attr, accessor=None, default=missing):
return obj This let me use this schema: class SampleFilterSchema(with_metaclass(SchemaMeta, BaseModelSchema)):
class Meta:
model = SampleFilter
id = fields.Integer(attribute='sample_filter_id')
type = fields.Constant('filter')
attributes = SelfNested(Schema.from_dict(dict(
tag=fields.String(attribute='sample_filter_tag'),
name=fields.String(attribute='sample_filter_name'),
public=fields.Boolean(attribute='is_public'),
data=JsonString(attribute='sample_filter_data'),
user=ResourceHyperlink(endpoint='rest_api.user', url_args=[
'user_id',
])
))) (ignore my custom fields, they're not important for this example) Which transforms: {
"user_id":1,
"is_public":false,
"sample_filter_name":"TestFilter",
"sample_filter_data":"",
"sample_filter_tag":"Global",
"sample_filter_id":1
} into {
"id":1,
"attributes":{
"user":"/rest_api/v1/users/1",
"tag":"Global",
"public":false,
"name":"TestFilter",
"data": ""
}
} aka, a valid response (or segment thereof) under the JSON API standard! |
There's discussion on #1315 (comment) to allow |
As described in #900, there is no obvious way to create a nested serialization from a flat object. To fix this,
fields.Nested
should accept an optionalfrom_parent
argument. When true, the nested Schema is passed the parent object rather than the attribute at the field name.from_parent
will default toFalse
, which will maintain current behavior.Any feedback on this idea? I plan on implementing it soon.
Thanks,
James
The text was updated successfully, but these errors were encountered: