Skip to content

Commit b85b8bc

Browse files
authored
Bugfix: fixes for sqlalchemy (#85)
* Added new setting to configure CROP and bump packages. Enhance doc and examples * Fixes for sqlalchemy
1 parent 9fe293f commit b85b8bc

8 files changed

Lines changed: 1455 additions & 1302 deletions

File tree

docs/build.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def read_cls_docstring(cls):
3939

4040
def get_versions():
4141
return [
42+
{
43+
"version": "0.2.17",
44+
"changes": [
45+
"Fixes for fk sqlalchemy postgres. Convert str to int for them.",
46+
],
47+
},
4248
{
4349
"version": "0.2.16",
4450
"changes": [

docs/index.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

fastadmin/models/orms/sqlalchemy.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import contextlib
12
from typing import Any
23
from uuid import UUID
34

4-
from sqlalchemy import and_, func, inspect, or_, select, text
5+
from sqlalchemy import BIGINT, Integer, and_, func, inspect, or_, select, text
56
from sqlalchemy.orm import selectinload
67

78
from fastadmin.models.base import InlineModelAdmin, ModelAdmin
@@ -261,21 +262,27 @@ def convert_sort_by(sort_by: str) -> str:
261262
for field_with_condition, value in filters.items():
262263
field = field_with_condition[0]
263264
condition = field_with_condition[1]
265+
model_field = getattr(self.model_cls, field)
266+
267+
if isinstance(model_field.expression.type, BIGINT | Integer):
268+
with contextlib.suppress(ValueError):
269+
value = int(value)
270+
264271
match condition:
265272
case "lte":
266-
q.append(getattr(self.model_cls, field) >= value)
273+
q.append(model_field >= value)
267274
case "gte":
268-
q.append(getattr(self.model_cls, field) <= value)
275+
q.append(model_field <= value)
269276
case "lt":
270-
q.append(getattr(self.model_cls, field) > value)
277+
q.append(model_field > value)
271278
case "gt":
272-
q.append(getattr(self.model_cls, field) < value)
279+
q.append(model_field < value)
273280
case "exact":
274-
q.append(getattr(self.model_cls, field) == value)
281+
q.append(model_field == value)
275282
case "contains":
276-
q.append(getattr(self.model_cls, field).like(f"%{value}%"))
283+
q.append(model_field.like(f"%{value}%"))
277284
case "icontains":
278-
q.append(getattr(self.model_cls, field).ilike(f"%{value}%"))
285+
q.append(model_field.ilike(f"%{value}%"))
279286
qs = qs.filter(and_(*q))
280287

281288
if search and self.search_fields:
@@ -313,13 +320,26 @@ async def orm_get_obj(self, id: UUID | int) -> Any | None:
313320
async with sessionmaker() as session:
314321
return await session.get(self.model_cls, id)
315322

323+
def _get_foreign_key_fields(self) -> list[str]:
324+
"""Returns a list of foreign key fields for the model.
325+
326+
:return: List of foreign key field names.
327+
"""
328+
return [column.name for column in self.model_cls.__table__.columns if column.foreign_keys]
329+
316330
async def orm_save_obj(self, id: UUID | Any | None, payload: dict) -> Any:
317331
"""This method is used to save orm/db model object.
318332
319333
:params id: an id of object.
320334
:params payload: a dict of payload.
321335
:return: An object.
322336
"""
337+
for fk_field_name in self._get_foreign_key_fields():
338+
if fk_field_name in payload and isinstance(payload[fk_field_name], str):
339+
with contextlib.suppress(ValueError):
340+
# convert string to int for foreign key fields for postgresql alchemy
341+
payload[fk_field_name] = int(payload[fk_field_name])
342+
323343
sessionmaker = self.get_sessionmaker()
324344
async with sessionmaker() as session:
325345
if id:

fastadmin/static/assets/worker-MF2p-l5_.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fastadmin/static/index.min.js

Lines changed: 215 additions & 215 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)