-
Hi, is there a way to add dynamic column? I tried adding property, but it's not working
Exception is
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hey, what is the purpose of that dynamic property? Is it only for displaying calculated data? |
Beta Was this translation helpful? Give feedback.
-
I found a hacky solution, which could easily break in later versions, but for my usecase it's ok for now Here it is: import sqlmodel
from sqladmin import ModelAdmin
from sqlalchemy import Column
from app.database import Test
class Test(sqlmodel.SQLModel, table=True):
id: Optional[int] = sqlmodel.Field(default=None, primary_key=True)
name: str
@property
def random(self) -> float:
return random.random()
class DynamicColumn(Column):
def __init__(self, name):
super().__init__(key=name, name=name)
class TestAdmin(ModelAdmin, model=Test):
column_list = [
Test.id,
Test.name,
'random',
]
column_labels = {'random': 'random'}
_column_labels_value_by_key = {'random': DynamicColumn('random')} |
Beta Was this translation helpful? Give feedback.
-
@aminalaee, I want to make PR to make it less hacky, what do you think about such design? import sqlmodel
from sqladmin import ModelAdmin
from app.database import Test
class Test(sqlmodel.SQLModel, table=True):
id: Optional[int] = sqlmodel.Field(default=None, primary_key=True)
name: str
@property
def random(self) -> float:
return random.random()
class TestAdmin(ModelAdmin, model=Test):
column_list = [
Test.id,
Test.name,
'random',
]
dynamic_columns = ['random'] |
Beta Was this translation helpful? Give feedback.
I found a hacky solution, which could easily break in later versions, but for my usecase it's ok for now
Here it is: