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

Allow include/exclude columns in collections only or individual instances only #87

Open
guotie opened this issue Aug 3, 2012 · 6 comments

Comments

@guotie
Copy link

guotie commented Aug 3, 2012

for example, i have a Topic class:

class Topic:
    id = Column()
    user = Column(Integer, ForeignKey())
    title = Column()
    replies = Column(Integer, ForeignKey())
    content = Column()
    publish_tm = Column()

then, i create the api:
topic_api = manager.create_api(Topic)

How to implement this requirement:

  1. when i visit http://host:port/api/topic, I want the response of topics do not include content & replies
  2. when I visit http://host:port/api/topic/, I want the response of topic include content & replies.
  3. If i want query topics of a user by username, How can i make the query?
@jfinkels
Copy link
Owner

jfinkels commented Aug 9, 2012

In other words, you want to be able to specify both the columns to include when making a GET request to the complete collection at api/topic and the columns to include when making a GET request to a single instance at api/topic/1. Is that right? That feature does not yet exist, though I'm happy to review any pull requests for it.

I think your final question is a separate issue, namely allowing more complex search queries (by filtering on fields of related models), which I am not necessarily eager to add. Please create a separate issue (with a descriptive title) which describes the functionality you want.

@guotie
Copy link
Author

guotie commented Aug 9, 2012

question 3 is solved by filters. Thanks.

@guotie guotie closed this as completed Aug 9, 2012
@jfinkels jfinkels reopened this Aug 9, 2012
@jfinkels
Copy link
Owner

jfinkels commented Aug 9, 2012

I'd like to leave this open, because this is still a feature request for specifying include/exclude columns on both a per collection and per instance basis.

@Svenito
Copy link
Contributor

Svenito commented Sep 18, 2013

Indeed, I would also like to be able to define different include/exclude columns based on whether the request is for a collection or individual resource.

so GET to /api/users shows id, username, email for example, but a GET to /api/users/2 would give me a different set of fields.

@Svenito
Copy link
Contributor

Svenito commented Sep 18, 2013

I've made a start on this and want to post this commit to see if this is heading in the right direction or is acceptable. Will require some changes to the existing unit tests of course.

commit is: 2f4e462

jfinkels added a commit that referenced this issue Feb 19, 2015
Before the behavior of Flask-Restless was a bit arbitrary. Now we force
it to comply with a concrete (though still changing) specification,
which can be found at http://jsonapi.org/.

This is a (severely) backwards-incompatible change, as it changes which
API endpoints are exposed and the format of requests and responses.

This fixes (or at least makes it much easier to fix or much easier to
mark as "won't fix") several issues, including but not limited to

  - #87
  - #153
  - #168
  - #193
  - #208
  - #211
  - #213
  - #243
  - #252
  - #253
  - #258
  - #261
  - #262
  - #303
  - #394
jfinkels added a commit that referenced this issue Feb 23, 2015
Previously, the behavior of Flask-Restless was a bit arbitrary. Now we
force it to comply with a concrete (though still changing)
specification, which can be found at http://jsonapi.org/.

This is a (severely) backwards-incompatible change, as it changes which
API endpoints are exposed and the format of requests and responses.

This change also moves JSON API compliance tests to a convenient
distinct test module, `tests.test_jsonapi.py`, so that compliance with
the specification can be easily verified. These tests correspond to
version 1.0rc2 of the JSON API specification, which can be found in
commit json-api/json-api@af5dfcc.

This change fixes (or at least makes it much easier to fix or much
easier to mark as "won't fix") quite a few issues, including but not
limited to

  - #87
  - #153
  - #168
  - #193
  - #208
  - #211
  - #213
  - #243
  - #252
  - #253
  - #258
  - #261
  - #262
  - #303
  - #394
jfinkels added a commit that referenced this issue Feb 24, 2015
Previously, the behavior of Flask-Restless was a bit arbitrary. Now we
force it to comply with a concrete (though still changing)
specification, which can be found at http://jsonapi.org/.

This is a (severely) backwards-incompatible change, as it changes which
API endpoints are exposed and the format of requests and responses.

This change also moves JSON API compliance tests to a convenient
distinct test module, `tests.test_jsonapi.py`, so that compliance with
the specification can be easily verified. These tests correspond to
version 1.0rc2 of the JSON API specification, which can be found in
commit json-api/json-api@af5dfcc.

This change fixes (or at least makes it much easier to fix or much
easier to mark as "won't fix") quite a few issues, including but not
limited to

  - #87
  - #153
  - #168
  - #193
  - #208
  - #211
  - #213
  - #243
  - #252
  - #253
  - #258
  - #261
  - #262
  - #303
  - #394
jfinkels added a commit that referenced this issue Mar 3, 2015
Previously, the behavior of Flask-Restless was a bit arbitrary. Now we
force it to comply with a concrete (though still changing)
specification, which can be found at http://jsonapi.org/.

This is a (severely) backwards-incompatible change, as it changes which
API endpoints are exposed and the format of requests and responses.

This change also moves JSON API compliance tests to a convenient
distinct test module, `tests.test_jsonapi.py`, so that compliance with
the specification can be easily verified. These tests correspond to
version 1.0rc2 of the JSON API specification, which can be found in
commit json-api/json-api@af5dfcc.

This change fixes (or at least makes it much easier to fix or much
easier to mark as "won't fix") quite a few issues, including but not
limited to

  - #87
  - #153
  - #168
  - #193
  - #208
  - #211
  - #213
  - #243
  - #252
  - #253
  - #258
  - #261
  - #262
  - #303
  - #394
jfinkels added a commit that referenced this issue Mar 5, 2015
Previously, the behavior of Flask-Restless was a bit arbitrary. Now we
force it to comply with a concrete (though still changing)
specification, which can be found at http://jsonapi.org/.

This is a (severely) backwards-incompatible change, as it changes which
API endpoints are exposed and the format of requests and responses.

This change also moves JSON API compliance tests to a convenient
distinct test module, `tests.test_jsonapi.py`, so that compliance with
the specification can be easily verified. These tests correspond to
version 1.0rc2 of the JSON API specification, which can be found in
commit json-api/json-api@af5dfcc.

This change fixes (or at least makes it much easier to fix or much
easier to mark as "won't fix") quite a few issues, including but not
limited to

  - #87
  - #153
  - #168
  - #193
  - #208
  - #211
  - #213
  - #243
  - #252
  - #253
  - #258
  - #261
  - #262
  - #303
  - #394
jfinkels added a commit that referenced this issue Mar 7, 2015
Previously, the behavior of Flask-Restless was a bit arbitrary. Now we
force it to comply with a concrete (though still changing)
specification, which can be found at http://jsonapi.org/.

This is a (severely) backwards-incompatible change, as it changes which
API endpoints are exposed and the format of requests and responses.

This change also moves JSON API compliance tests to a convenient
distinct test module, `tests.test_jsonapi.py`, so that compliance with
the specification can be easily verified. These tests correspond to
version 1.0rc2 of the JSON API specification, which can be found in
commit json-api/json-api@af5dfcc.

This change fixes (or at least makes it much easier to fix or much
easier to mark as "won't fix") quite a few issues, including but not
limited to

  - #87
  - #153
  - #168
  - #193
  - #208
  - #211
  - #213
  - #243
  - #252
  - #253
  - #258
  - #261
  - #262
  - #303
  - #394
jfinkels added a commit that referenced this issue Mar 8, 2015
Previously, the behavior of Flask-Restless was a bit arbitrary. Now we
force it to comply with a concrete (though still changing)
specification, which can be found at http://jsonapi.org/.

This is a (severely) backwards-incompatible change, as it changes which
API endpoints are exposed and the format of requests and responses.

This change also moves JSON API compliance tests to a convenient
distinct test module, `tests.test_jsonapi.py`, so that compliance with
the specification can be easily verified. These tests correspond to
version 1.0rc2 of the JSON API specification, which can be found in
commit json-api/json-api@af5dfcc.

This change fixes (or at least makes it much easier to fix or much
easier to mark as "won't fix") quite a few issues, including but not
limited to

  - #87
  - #153
  - #168
  - #193
  - #208
  - #211
  - #213
  - #243
  - #252
  - #253
  - #258
  - #261
  - #262
  - #303
  - #394
@jfinkels
Copy link
Owner

This can now be fixed by defining a custom serializer class, introduced in pull request #510, something like this (untested) code.

class MySerializer(DefaultSerializer):

    def serialize(instance, only=None, *args, **kw):
        old_exclude = self.exclude
        try:
            self.exclude += [...]
            result = super(MySerializer, self).serialize(instance, only=only, *args, **kw)
        finally:
            self.exclude = old_exclude
        return result

    def serialize_many(instances, only=None, *args, **kw):
        # do something similar here

APIManager.create_api(Person, serializer_class=MySerializer)

It's not a great solution, especially since self.exclude is undocumented, but I believe it should work. There may be other workarounds as well.

This needs to be added to the documentation and probably as an example in the examples/ directory as well.

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

No branches or pull requests

3 participants