Skip to content

Commit 3077299

Browse files
committed
Added display and actions interfaces
1 parent 58c96c1 commit 3077299

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

README.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ FastAdmin is designed to be minimalistic, functional, yet familiar, to ease the
3636

3737
First you have to install FastAdmin like this:
3838

39-
```
39+
```bash
4040
pip install fastadmin
4141
```
4242

4343
or using poetry
4444

45-
```
45+
```bash
4646
poetry install fastadmin
4747
```
4848

@@ -55,7 +55,7 @@ Use prefix "/admin" as default for now. You can change it later.
5555

5656
Example:
5757

58-
```
58+
```python
5959
from fastapi import FastAPI
6060
from fastadmin import admin_app
6161

@@ -76,7 +76,7 @@ Setup the following env variables to configure FastAdmin (add to .env or export
7676

7777
Example:
7878

79-
```
79+
```bash
8080
export ADMIN_USER_MODEL = User
8181
export ADMIN_USER_MODEL_USERNAME_FIELD = username
8282
export ADMIN_SECRET_KEY = secret_key
@@ -92,7 +92,7 @@ Implement an authenticate method for ModelAdmin with registered model ADMIN_USER
9292

9393
Example (for Tortoise ORM):
9494

95-
```
95+
```python
9696
import bcrypt
9797
from tortoise.models import Model
9898
from fastadmin import TortoiseModelAdmin, register
@@ -102,6 +102,7 @@ class User(Model):
102102
username = fields.CharField(max_length=255, unique=True)
103103
hash_password = fields.CharField(max_length=255)
104104
is_superuser = fields.BooleanField(default=False)
105+
is_active = fields.BooleanField(default=False)
105106

106107
...
107108

@@ -132,7 +133,7 @@ class GroupAdmin(TortoiseModelAdmin):
132133

133134
Run your project (see https://fastapi.tiangolo.com/tutorial/first-steps/):
134135

135-
```
136+
```bash
136137
uvicorn ...
137138
```
138139

@@ -146,15 +147,30 @@ You can find all parameters and methods to configure your ModelAdmin classes [he
146147

147148
Example:
148149

149-
```
150+
```python
151+
from fastadmin import TortoiseModelAdmin, register, action, display
152+
150153
@register(User)
151154
class UserAdmin(TortoiseModelAdmin):
155+
label_fields = ("email", "id")
152156
exclude = ("hash_password",)
153-
list_display = ("id", "username")
157+
list_display = ("id", "email", "has_hash_password", "is_superuser", "is_active")
158+
list_display_links = ("id",)
159+
list_filter = ("id", "email", "is_superuser")
160+
search_fields = ("email",)
161+
actions = ("set_as_active",)
154162

155163
def has_delete_permission(self) -> bool:
156164
return False
157165

166+
@action(description="Set as active")
167+
async def set_as_active(self, ids: list[int | UUID]) -> None:
168+
await User.filter(id__in=ids).update(is_active=True)
169+
170+
@display
171+
async def has_hash_password(self, obj: Any) -> Any:
172+
return obj.hash_password is not None
173+
158174
...
159175
```
160176

@@ -164,31 +180,30 @@ We are going to support SQLAlchemy and Pony ORM soon...
164180

165181
If you have smth else (your own implementation of ORM and so on) you will may overload ModelAdmin class and implement the following interfaces
166182

167-
```
183+
```python
168184
from typing import Any
169185
from collections import OrderedDict
170186
from fastadmin import ModelAdmin, WidgetType
171187

172188
class MyModelAdmin(ModelAdmin):
173-
async def save_model(self, obj: Any, payload: dict, add: bool = False) -> None:
189+
async def save_model(self, id: UUID | int | None, payload: dict) -> dict | None:
174190
"""This method is used to save orm/db model object.
175191
176-
:params obj: an orm/db model object.
192+
:params id: an id of object.
177193
:params payload: a payload from request.
178-
:params add: a flag for add or update object.
179-
:return: None.
194+
:return: A saved object or None.
180195
"""
181196
raise NotImplementedError
182197

183-
async def delete_model(self, obj: Any) -> None:
198+
async def delete_model(self, id: UUID | int) -> None:
184199
"""This method is used to delete orm/db model object.
185200
186-
:params obj: an orm/db model object.
201+
:params id: an id of object.
187202
:return: None.
188203
"""
189204
raise NotImplementedError
190205

191-
async def get_obj(self, id: str) -> Any | None:
206+
async def get_obj(self, id: UUID | int) -> dict | None:
192207
"""This method is used to get orm/db model object by id.
193208
194209
:params id: an id of object.
@@ -203,7 +218,7 @@ class MyModelAdmin(ModelAdmin):
203218
search: str | None = None,
204219
sort_by: str | None = None,
205220
filters: dict | None = None,
206-
) -> tuple[list[Any], int]:
221+
) -> tuple[list[dict], int]:
207222
"""This method is used to get list of orm/db model objects.
208223
209224
:params offset: an offset for pagination.
@@ -231,7 +246,6 @@ class MyModelAdmin(ModelAdmin):
231246
:return: A tuple of widget type and widget props.
232247
"""
233248
raise NotImplementedError
234-
235249
```
236250

237251
## License

0 commit comments

Comments
 (0)