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

Make it easy to parse a Pagination object to dict #482

Closed
italomaia opened this issue Mar 4, 2017 · 9 comments
Closed

Make it easy to parse a Pagination object to dict #482

italomaia opened this issue Mar 4, 2017 · 9 comments

Comments

@italomaia
Copy link

Being able to easely parse a Pagination object to dict would make it much more "ajax" friendly.

@kalombos
Copy link
Contributor

Hello, i want to contribute but i didn't understand what you need. You want to realize additional method at Pagination class something like this:

def as_dct(self):
    return dict(items=self.items, page=self.page...)

?
Please give me more information and i will try to realize it. Thank you.

@ThiefMaster
Copy link
Contributor

Not dct for sure. That abbreviation is only OK when the name would be dict otherwise (and thus shadow the builtin). I'd go for something like to_dict or as_dict.

@protream
Copy link

protream commented Apr 12, 2017

We can do it like this, add two methods to class Pagination:

class Pagination:
    ...

    def __getitem__(self, key):
        return getattr(self, key)

    def keys(self):
        return ('page', 'pages', 'total', 'has_prev', 'next_num', 'has_next', 'items')

Now, dict a Pagination Object jus do:

data = dict(pagination)
data['items'] = some_handle_for(data['items'])

Since items may be not JSON serializable, so it should be handle separately.

@spyoungtech
Copy link
Contributor

Wouldn't it be most appropriate to implement a __iter__ for this? That way one can simply do dict(my_pagination_object)

@nicolashahn
Copy link

@protream's implementation as a PR: #580

@rsyring rsyring added this to the 3.x milestone Mar 9, 2019
@rtilk89
Copy link

rtilk89 commented May 31, 2019

@jab @davidism - I'm interested in giving this issue a shot, but looks like there is an unmerged PR. Is this still open for contributions?

@tomowind
Copy link

@rtilk89 I was reading two unmerged PRs #485 and #580. #485 doesn't address comments fully, and #580 doesn't seem to incorporate comments in #485. So, you may extend #580 with tests and addressing comments in #485?

You can work on this one. I will do another.

@davidism
Copy link
Member

davidism commented Jun 2, 2019

The problem with an as_dict method is that it does not guarantee that the result will be serializable by jsonify or json.dumps. Result rows can contain types that are not handled by the default JSON serializer, and it gets even worse when querying models, and it gets even worse when those models are not simple but use the multitude of features available to SQLAlchemy.

The direct way for a dev to handle this, although not the most powerful, is to implement their own Flask.json_encoder class to handle the things that would appear in Pagination.items. So if we do add a as_dict method, it needs to clearly explain that the developer still needs to do something else.

We do not want to take on maintaining a serialization library for result rows or SQLAlchemy models. This is better handled by a serialization library such as Marshmallow, Marshmallow-SQLAlchemy, and Marshmallow-Flask, or another API extension. At which point, that can also handle serializing a Pagination object, along with any other API data, at which point as_dict isn't needed.

@davidism
Copy link
Member

davidism commented Jun 3, 2019

Closing based on above comment and discussion in #748.

@davidism davidism closed this as completed Jun 3, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 5, 2020
@davidism davidism removed this from the 3.x milestone Sep 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests