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

Query to only return specific fields set #180

Open
matdrapeau opened this issue Apr 28, 2020 · 1 comment
Open

Query to only return specific fields set #180

matdrapeau opened this issue Apr 28, 2020 · 1 comment

Comments

@matdrapeau
Copy link

In order to reduce the amount of data being transferred from a resource, is it possible to provide a query args to return a set of fields?
Sometimes, we don't need all attributes of an object but a couple of them.
It would require too many custom routes to expose the different set of attributes we would need.
Here a few examples to describe it:

/users?include_fields=['first_name', 'last_name']
/users?include_fields=['email']
/users?include_fields=['city', 'country']
@kvalv
Copy link

kvalv commented Jul 27, 2020

For a single instance, maybe this would do:

from flask_potion import routes, fields
@routes.ItemRoute.GET
def filtered(self, item, fields: fields.Array(fields.String)):
    return {f: getattr(item, f) for f in fields}

And pass the attributes you want to include in the body of the GET request. Not sure if it's the best practice (and probably way too late response anyway), but I guess it's an option 🤷

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