In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the use of the flask sqlalchemy expansion package". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The flask_sqlalchemy expansion pack simply encapsulates the ORM framework SQLAlchemy and supports the paging operation after encapsulating the Query object, that is, the flask_sqlalchemy.BaseQuery.paginate method. The corresponding Pagination object will be returned when the number of pages and the size of each page are passed in. For example:
Pagination = Article.query.join (Account)\
.filter (Article.status = = 1)\
.order _ by (Article.published_at.desc ())\
Paginate (page, per_page)
There is data in the Pagination object and the total number of data items. This saves you a few lines of code than implementing your own paging, and then you encounter a weird problem when you restructure the code with paginate. Found that access to the paging parameter URL reported 404, at first thought that I entered the URL is incorrect, and then print the url_map inside value, there is nothing wrong with the comparison. I tried to get rid of paginate and revert to the original limit and offset methods to control paging, which returned to normal. So I'm sure it's the paginate method, and then I find the source code in it, and I find that the method has an error_out parameter.
Def paginate (self, page=None, per_page=None, error_out=True, max_per_page=None):
"" Returns ``per_ page``items from page ``page``.
If ``page``or ``per_ page``are ``None``, they will be retrieved from
The request query. If ``max_per_ page`` is specified, ``per_ page`` will
Be limited to that value. If there is no request or they aren't in the
Query, they default to 1 and 20 respectively.
When ``error_ out`` is ``True`` (default), the following rules will
Cause a 404 response:
* No items are found and ``page`` is not 1.
* ``page``is less than 1, or ``per_ page`` is negative.
* ``page``or ``per_ page`` are not ints.
When ``error_ out`` is ``False``, ``page``and ``per_ page`` default to
1 and 20 respectively.
Returns a: class: `Pagination` object.
"
If request:
If page is None:
Try:
Page = int (request.args.get ('page', 1))
Except (TypeError, ValueError):
If error_out:
Abort (404)
Page = 1
If per_page is None:
Try:
Per_page = int (request.args.get ('per_page', 20))
Except (TypeError, ValueError):
If error_out:
Abort (404)
Per_page = 20
Else:
If page is None:
Page = 1
If per_page is None:
Per_page = 20
If max_per_page is not None:
Per_page = min (per_page, max_per_page)
If page < 1:
If error_out:
Abort (404)
Else:
Page = 1
If per_page < 0:
If error_out:
Abort (404)
Else:
Per_page = 20
Items = self.limit (per_page) .offset ((page-1) * per_page) .all ()
If not items and page! = 1 and error_out:
Abort (404)
The default value of error_out is True, which means that any abnormal behavior will cause a 404 response. For example, if the page parameter you passed is not a number, it will report 404, if it is less than 1, it will report 404, and if there is no data, it will report 404. I am writing the API interface, you give me a report of 404, too fucking unkind.
As a third-party library, I don't think it's appropriate for developers to make decisions by default, because your original intention may be dont make me think, but it makes me ponder, because I don't know why I reported 404 and I can't see any stack logs. A more reasonable way is to inform developers in an abnormal way. This way I know how to modify the parameters to return the desired results. In fact, this is the same as the product manager designing the product, we need to hide all the technical details from the end user, and any error log should not be shown to the user, because the user does not understand and does not need to let it understand. Error logs are very important to developers, and if I hide them for me, I have to go deep into the source code to find out why.
This is the end of the content of "what is the use of the flask sqlalchemy expansion Pack"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.