Several pagination methods of SQLAlchemy and Flask-sqlalchemy

Source: Internet
Author: User
Tags mongodb postgresql redis


Query queries are used in SQLAlchemy, and basequery queries are used in Flask-sqlalchemy, and they are the relationships between subclasses and parent classes



Suppose page_index=1,page_size=10; All paged queries cannot be followed by first (), all (), etc.


1.sets the index offset with offset (), limit () limits the amount taken




Db.session.query (User.Name). Filter (User.email.like ('%'+email+'% ')). Limit (page_size). Offset ((page_index-1) *page_size)#Thefilter statement can be followed by the order_by statement
2. Use Slice (offset, amount of removal ) function




Db.session.query (User.Name). Filter (User.email.like ('%'+email+'% ')). Slice ((page_index-1) * page_size, Page_index * page_size)#Thefilter statement can be followed by the order_by statement


Note: This method has the same effect as the first.



Because: By the internal method, the first property of the slice () function is the value of the offset () function, and the second property is the value of the limit () function.


 
@_generative(_no_statement_condition)
    def slice(self, start, stop):
        """apply LIMIT/OFFSET to the ``Query`` based on a "
        "range and return the newly resulting ``Query``."""

        if start is not None and stop is not None:
            self._offset = (self._offset or 0) + start
            self._limit = stop - start
        elif start is None and stop is not None:
            self._limit = stop
        elif start is not None and stop is None:
            self._offset = (self._offset or 0) + start

        if self._offset == 0:
            self._offset = None

    @_generative(_no_statement_condition)
    def limit(self, limit):
        """Apply a ``LIMIT`` to the query and return the newly resulting

        ``Query``.

        """
        self._limit = limit

    @_generative(_no_statement_condition)
    def offset(self, offset):
        """Apply an ``OFFSET`` to the query and return the newly resulting
        ``Query``.

        """
        self._offset = offset
3. Use Paginate (offset, amount of removal function for Basequery




User_obj=user.query.filter (User.email.like ('%'+email+'%' ) . paginate (int (page_index), int (page_size), False)#object_list =user_ Obj.items



4.use limit in filter




Db.session.query (User.Name). Filter (User.email.like ('%'+email+'% '  and limit (page_index-1) * page_size, page_size)# here can not be followed order_by statement, otherwise error








Http://www.cnblogs.com/rgcLOVEyaya/p/RGC_LOVE_YAYA_350days.html






Several pagination methods of SQLAlchemy and Flask-sqlalchemy


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.