Skip to content

Dynamic fields #219

Answered by cuamckuu
cuamckuu asked this question in Q&A
Jun 27, 2022 · 3 comments · 2 replies
Discussion options

You must be logged in to vote

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': '…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@cuamckuu
Comment options

@aminalaee
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by cuamckuu
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants