Replies: 2 comments 2 replies
-
starlette-admin doesn't have built-in support for Tortoise ORM at the moment. However, you can extend the BaseModelView to create a custom view that works with Tortoise ORM Documentation: https://jowilf.github.io/starlette-admin/advanced/base-model-view/ |
Beta Was this translation helpful? Give feedback.
2 replies
-
I created a view class for Tortoise orm 🐢 class TortoiseModelView(BaseModelView):
def __init__(self, obj: Type[Model]):
super().__init__()
self.obj = obj
async def find_all(self, request: Request, skip: int = 0, limit: int = 100,
where: Union[Dict[str, Any], str, None] = None,
order_by: Optional[List[str]] = None) -> Sequence[Any]:
if (where is not None) and isinstance(where, dict):
query_objs = self.obj.filter(**where).all()
else:
query_objs = self.obj.all()
if order_by is not None:
"""Multiple sort"""
ord_vals = []
for item in reversed(order_by):
key, dirc = item.split(maxsplit=1)
ord_val = "-" if dirc == "desc" else "" + key
ord_vals.append(ord_val)
query_objs = query_objs.order_by(*ord_vals)
objs = await query_objs
if limit > 0:
return objs[skip:skip+limit]
return objs[skip:]
async def count(self, request: Request, where: Union[Dict[str, Any], str, None] = None) -> int:
if where is not None:
if isinstance(where, dict):
query_objs = self.obj.filter(**where)
else:
query_objs = self.obj.filter(pk=where)
return await query_objs.count()
return await self.obj.all().count()
async def find_by_pk(self, request: Request, pk: int) -> Any:
pk_obj = await self.obj.filter(pk=int(pk)).first()
if pk_obj:
return pk_obj
return None
async def find_by_pks(self, request: Request, pks: List[int]) -> Sequence[Any]:
return await self.obj.filter(pk__in=pks).all()
async def create(self, request: Request, data: Dict) -> Any:
return await self.obj.create(**data)
async def edit(self, request: Request, pk: Any, data: Dict[str, Any]) -> Any:
await self.obj.filter(pk=pk).update(**data)
return await self.obj.filter(pk=pk).first()
async def delete(self, request: Request, pks: List[int]) -> Optional[int]:
objs = await self.obj.filter(pk__in=pks).all()
await self.obj.filter(pk__in=pks).delete()
return objs.__len__() In admin.py class UserView(TortoiseModelView):
identity = "user"
name = "User"
label = "Users"
icon = "fa fa-user"
pk_attr = "id"
fields = [
IntegerField("id"),
StringField("chat_id"),
StringField("country"),
StringField("referral_code")
] In app.py from admin import UserView
admin = BaseAdmin()
admin.add_view(UserView(User))
admin.mount_to(app) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is Starlette admin compatible with Tortoise ORM?
Beta Was this translation helpful? Give feedback.
All reactions