Skip to content

Pages and rows

The following methods allow you to paginate and limit the number of rows in a query.

  • paginate(page: int) -> QuerySet

  • limit(limit_count: int) -> QuerySet

  • offset(offset: int) -> QuerySet

  • get() -> Model

  • First () -> Model

  • query set proxy

    • QuerysetProxy.paginate(page: int) method
    • QuerysetProxy.limit(limit_count: int) method
    • QuerysetProxy.offset(offset: int) method

paging

paginate(page: int, page_size: int = 20) -> QuerySet

Combines offset and restriction methods based on page number and size

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Track(ormar.Model):
    ormar_config = ormar.OrmarConfig(
        database=databases.Database(DATABASE_URL),
        metadata=sqlalchemy.MetaData(),
        tablename="track"
    )

    id: int = ormar.Integer(primary_key=True)
    album: Optional[Album] = ormar.ForeignKey(Album)
    name: str = ormar.String(max_length=100)
    position: int = ormar.Integer()


tracks = await Track.objects.paginate(3).all()
# will return 20 tracks starting at row 41 
# (with default page size of 20)

Note that paginate(2) is equivalent to offset(20).limit(20)

impose restrictions on

limit(limit_count: int, limit_raw_sql: bool = None) -> QuerySet

You can limit the results to the number of parent models you want.

To limit the actual number of database query rows rather than the number of main models, use the limit_raw_sql parameter flag and set it to True.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Track(ormar.Model):
    ormar.OrmarConfig(
        database=databases.Database(DATABASE_URL),
        metadata=sqlalchemy.MetaData(),
        tablename="track"
    )

    id: int = ormar.Integer(primary_key=True)
    album: Optional[Album] = ormar.ForeignKey(Album)
    name: str = ormar.String(max_length=100)
    position: int = ormar.Integer()


tracks = await Track.objects.limit(1).all()
# will return just one Track

Note

So operations like filter(), select_related(), limit() and offset() etc. can be chained.

Something like Track.object.select_related("album").filter(album__name="Malibu").offset(1).limit(1).all()

counteract

offset(offset: int, limit_raw_sql: bool = None) -> QuerySet

You can also offset the results by the number of master models required.

To offset the actual number of database query rows rather than the number of main model rows, use the limit_raw_sql parameter flag and set it to True.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Track(ormar.Model):
    ormar.OrmarConfig(
        database=databases.Database(DATABASE_URL),
        metadata=sqlalchemy.MetaData(),
        tablename="track"
    )

    id: int = ormar.Integer(primary_key=True)
    album: Optional[Album] = ormar.ForeignKey(Album)
    name: str = ormar.String(max_length=100)
    position: int = ormar.Integer()


tracks = await Track.objects.offset(1).limit(1).all()
# will return just one Track, but this time the second one

get

get(**kwargs) -> Model

Get the first row from the database that satisfies the kwargs setting criteria.

If no condition is set, it returns the last row sorted by pk in db.(Filter/exclude also cannot be used to set conditions).

!!!tip To learn more about get, visit read/get

number one

First () -> Model

Gets the first row sorted in ascending order by primary key column from the database.

!!!tip To learn more about your first visit, read/first

QuerysetProxy method

Returns a list of related models when directly accessing the related ManyToMany field and ReverseForeignKey.

But it also exposes a subset of the QuerySet API, so you can filter, create, select related models, and so on directly from the parent model.

paging

Same pagination functionality as above, but allows you to paginate related objects from the other end of the relationship.

!!!tip To learn more about QuerysetProxy, visit the QuerysetProxy section

impose restrictions on

Identical to the limit function above, but allows you to paginate related objects from the other side of the relationship.

!!!tip To learn more about QuerysetProxy, visit the QuerysetProxy section

counteract

Identical to the offset function above, but allows you to paginate related objects from the other side of the relationship.

!!!tip To learn more about QuerysetProxy, visit the QuerysetProxy section