Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ajax.py: get object by PK on form rendering #816

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion sqladmin/ajax.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Optional

from sqlalchemy import String, cast, inspect, or_, select

Expand Down Expand Up @@ -59,8 +59,21 @@ def format(self, model: type) -> dict[str, Any]:
if not model:
return {}

if isinstance(model, str):
model = self.get_object(model)

return {"id": str(get_object_identifier(model)), "text": str(model)}

def get_object(self, pk: str) -> Optional[type]:
pk_attr = get_object_identifier(self.model)
stmt = select(self.model).filter(pk_attr == pk)

result = self.model_admin._run_query_sync(stmt)
if not result:
return None

return result

async def get_list(self, term: str) -> list[Any]:
stmt = select(self.model)

Expand Down
Loading