You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the `AsyncPaginator` class is an async version of django's [Paginator](https://docs.djangoproject.com/en/5.1/ref/paginator/#django.core.paginator.Paginator).
4
+
5
+
although `AsyncPaginator` inherits from django's `Paginator`, but this is only for code reuse and easier migration from sync to async, and you should not call the sync methods from an async environment.
6
+
7
+
the paginator can take a queryset or a list, but they have slightly different behaviours,
8
+
if you pass in a queryset to the paginator, the queryset is preserved until it has to be evaluated, so until it is evaluated it needs to be treated as a queryset,
9
+
and using querysets in an async environment is different from using querysets in sync environments.
10
+
11
+
### Examples
12
+
13
+
**note**: in normal django shell you can not `await`, to use `await` you need a shell tht supports that, such as [ipython](https://ipython.org/)
14
+
15
+
#### Example of list pagination
16
+
17
+
```pycon
18
+
In [1]: from django_async_extensions.acore.paginator import AsyncPaginator
19
+
20
+
In [2]: objects = ["john", "paul", "george", "ringo"]
21
+
22
+
In [3]: p = AsyncPaginator(objects, 2)
23
+
24
+
In [4]: await p.acount()
25
+
Out[4]: 4
26
+
27
+
In [5]: await p.anum_pages()
28
+
Out[5]: 2
29
+
30
+
In [6]: type(await p.apage_range())
31
+
Out[6]: range
32
+
33
+
In [7]: await p.apage_range()
34
+
Out[7]: range(1, 3)
35
+
36
+
In [8]: page1 =await p.apage(1)
37
+
38
+
In [9]: page1
39
+
Out[9]: <Async Page 1>
40
+
41
+
In [10]: page1.object_list
42
+
Out[10]: ['john', 'paul']
43
+
44
+
In [11]: page2 =await p.apage(2)
45
+
46
+
In [12]: page2.object_list
47
+
Out[12]: ['george', 'ringo']
48
+
49
+
In [13]: await page2.ahas_next()
50
+
Out[13]: False
51
+
52
+
In [14]: await page2.ahas_previous()
53
+
Out[14]: True
54
+
55
+
In [15]: await page2.ahas_other_pages()
56
+
Out[15]: True
57
+
58
+
await page2.anext_page_number()
59
+
Traceback (most recent call last):
60
+
...
61
+
EmptyPage: That page contains no results
62
+
63
+
In [17]: await page2.aprevious_page_number()
64
+
Out[17]: 1
65
+
66
+
In [18]: await page2.astart_index()
67
+
Out[18]: 3
68
+
69
+
In [19]: await page2.aend_index()
70
+
Out[19]: 4
71
+
72
+
In [20]: await p.apage(0)
73
+
Traceback (most recent call last):
74
+
...
75
+
EmptyPage: That page number is less than 1
76
+
77
+
In [21]: await p.apage(3)
78
+
Traceback (most recent call last):
79
+
...
80
+
EmptyPage: That page contains no results
81
+
```
82
+
83
+
84
+
#### Example of queryset pagination
85
+
86
+
```pycon
87
+
In [1]: from django.contrib.auth.models import User
88
+
89
+
In [2]: objs = [User(username=f"test{i}", password="testpass123") for i inrange
0 commit comments