Skip to content
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

Filter fields in embedded document #15

Closed
mapeebles opened this issue Jul 21, 2017 · 5 comments
Closed

Filter fields in embedded document #15

mapeebles opened this issue Jul 21, 2017 · 5 comments

Comments

@mapeebles
Copy link

mapeebles commented Jul 21, 2017

I think this is a feature request but is there a way to filter fields from embedded documents? Think of a base document (in mongodb) that looks like this in JSON:

[{
  "_id":  12,
  "name": "test 1",
  "embdded_doc": {
     "_id": 1237652,
     "other_info": "this and that",
     "color": "red"
  } 
}]

and you only want to output: _id, name, embedded_doc.color

We are trying to avoid having to serialize the entire embedded doc since it could a be large memory footprint, especially when we are returning a list of the parent objects.

Thanks

@dbrgn
Copy link
Owner

dbrgn commented Jul 26, 2017

This is not currently supported. Why don't you simply create a serializer that only serializes some of the fields?

@mapeebles
Copy link
Author

We were hoping not to have to create one for every API's different use cases. We have a lot of listing api's that only need a subset of the model and their child documents fields so we feel the right approach is to have a general purpose interface that can filter on the request thus no need extra development and testing on each model.

@dbrgn
Copy link
Owner

dbrgn commented Jul 26, 2017

I see. This library is intentionally pretty simple, it just filters the fields of the top-level serializer.

If you want to apply filters to nested serializers, that'll become a bit more complicated. If someone would provide a clean pull request with this feature and good test coverage, I wouldn't say no, but I don't have the capacity to add that feature right now.

Maybe https://github.com/rsinger86/drf-flex-fields already does what you are looking for?

@mapeebles
Copy link
Author

I had looked at that one, but they seem to acknowledge the existence of the child documents and allow for expansion but I didn't notice a good way to restrict the output, and it would be more complex if we have multiple child document in a parent document.

Thanks though, I appreciate you taking the time to review the request.

@dbrgn
Copy link
Owner

dbrgn commented Jul 26, 2017

Maybe you can create a feature request against drf-flex-fields. They seem to aim for more flexibilit than I do :)

Otherwise, writing custom code for the specific filtering needs might be quite simple to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants