Skip to content

Commit a419c2b

Browse files
committed
Update customization docs
1 parent 645a1a1 commit a419c2b

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

docs/tutorials_advanced/customization.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ new class objects and `mypy` does not support such ways.
2020
First, let's import all necessary components:
2121

2222
```py
23-
{!../docs_src/tutorials_advanced/customization.py [ln:1-9]!}
23+
{!../docs_src/tutorials_advanced/customization.py [ln:1-12]!}
2424
```
2525

2626
Now we can customize our page.
@@ -30,7 +30,7 @@ Now we can customize our page.
3030
If you want to change default name of page class, you should use `UseName` customizer:
3131

3232
```py
33-
{!../docs_src/tutorials_advanced/customization.py [ln:11-14]!}
33+
{!../docs_src/tutorials_advanced/customization.py [ln:14-17]!}
3434
```
3535

3636
1. Now your class will be names 'IntPage' instead of 'CustomizedPage'.
@@ -47,7 +47,7 @@ By default, cursor-based page don't include total count of items, and offset-bas
4747
If you want to change this behavior, you should use `UseIncludeTotal` customizer:
4848

4949
```py
50-
{!../docs_src/tutorials_advanced/customization.py [ln:16-19]!}
50+
{!../docs_src/tutorials_advanced/customization.py [ln:19-22]!}
5151
```
5252

5353
1. Now when you will paginate using `PageNoTotal` class, it will not include total count of items.
@@ -57,7 +57,7 @@ If you want to change this behavior, you should use `UseIncludeTotal` customizer
5757
If you want to change default values of pagination parameters, you should use `UseParamsFields` customizer:
5858

5959
```py
60-
{!../docs_src/tutorials_advanced/customization.py [ln:21-24]!}
60+
{!../docs_src/tutorials_advanced/customization.py [ln:24-27]!}
6161
```
6262

6363
1. Now when `size` parameter is not provided, it will be equal to 500.
@@ -68,18 +68,51 @@ If you want to change default values of pagination parameters, you should use `U
6868
If you want to change type of pagination parameters, you should use `UseParams` customizer:
6969

7070
```py
71-
{!../docs_src/tutorials_advanced/customization.py [ln:26-29]!}
71+
{!../docs_src/tutorials_advanced/customization.py [ln:29-32]!}
7272
```
7373

7474
1. Now all pagination parameters will be optional.
7575

7676

77+
### Change fields names
78+
79+
If you want use another name of field rather than default, you should use `UseFieldsAliases` customizer:
80+
81+
```py
82+
{!../docs_src/tutorials_advanced/customization.py [ln:43-46]!}
83+
```
84+
85+
1. Now `total` field will be serialized as `count`.
86+
87+
88+
### Exclude fields
89+
90+
If you want to exclude some fields from serialization, you should use `UseExcludedFields` customizer:
91+
92+
```py
93+
{!../docs_src/tutorials_advanced/customization.py [ln:48-51]!}
94+
```
95+
96+
1. Now `total` field will not be serialized.
97+
98+
99+
### Change pydantic model config
100+
101+
If you want to change pydantic model config, you should use `UseModelConfig` customizer:
102+
103+
```py
104+
{!../docs_src/tutorials_advanced/customization.py [ln:53-56]!}
105+
```
106+
107+
1. Now `Page` class will have `anystr_lower` set to `True`.
108+
109+
77110
### Change page params type
78111

79112
If you want to change type of page parameters, you should use `UseParams` customizer:
80113

81114
```py
82-
{!../docs_src/tutorials_advanced/customization.py [ln:31-39]!}
115+
{!../docs_src/tutorials_advanced/customization.py [ln:35-41]!}
83116
```
84117

85118
1. Now `Page.__params_type__` attribute will be point to `MyParams` class.
@@ -90,7 +123,7 @@ If you want to change type of page parameters, you should use `UseParams` custom
90123
You can use multiple customizers at once, just pass them as to regular `Annotated`:
91124

92125
```py
93-
{!../docs_src/tutorials_advanced/customization.py [ln:41-47]!}
126+
{!../docs_src/tutorials_advanced/customization.py [ln:58-63]!}
94127
```
95128

96129
1. Now `CustomPage` will have `CustomPage` name, no total count of items, all params optional.

docs_src/tutorials_advanced/customization.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from fastapi_pagination import Page, Params
22
from fastapi_pagination.customization import (
33
CustomizedPage,
4+
UseExcludedFields,
5+
UseFieldsAliases,
46
UseIncludeTotal,
7+
UseModelConfig,
58
UseName,
69
UseOptionalParams,
710
UseParams,
@@ -37,6 +40,21 @@ class MyParams(Params): ... # your magic here
3740
UseParams(MyParams), # (1)
3841
]
3942

43+
PageWithCount = CustomizedPage[
44+
Page,
45+
UseFieldsAliases(total="count"), # (1)
46+
]
47+
48+
PageWithoutTotal = CustomizedPage[
49+
Page,
50+
UseExcludedFields("total"), # (1)
51+
]
52+
53+
PageStrLower = CustomizedPage[
54+
Page,
55+
UseModelConfig(anystr_lower=True), # (1)
56+
]
57+
4058
CustomPage = CustomizedPage[
4159
Page,
4260
UseName("CustomPage"),

0 commit comments

Comments
 (0)