FastUI icon indicating copy to clipboard operation
FastUI copied to clipboard

Nested models

Open wpeterw opened this issue 1 year ago • 5 comments

I have a model like this (code below), how do I access for instance the name in TeamModel ?

This does not work:

columns=[
    DisplayLookup(field="position"),
    DisplayLookup(field="team.name"),

class TeamModel(BaseModel):
    id: int
    name: str
    shortName: str
    tla: str
    crest: str

class Standing(BaseModel):
    position: Optional[int] = None
    team: Dict[str, str|int]
    playedGames: Optional[int] = None
    form: Any
    won: Optional[int] = None
    draw: Optional[int] = None
    lost: Optional[int] = None
    points: Optional[int] = None

wpeterw avatar Dec 11 '23 06:12 wpeterw

You mean in a table?

I think that's not yet supported.

samuelcolvin avatar Dec 11 '23 13:12 samuelcolvin

yes, in a table, my data has nested objects.

wpeterw avatar Dec 11 '23 13:12 wpeterw

I think we could support that by using something like lodash.get (https://lodash.com/docs/4.17.15#get) in the frontend.

This way the backend can define any nested path to the data in the backend (e.g. teams.name or even array indexing like teams[0].names[1]) and the frontend tries to get the given path from the object.

Not sure if performance might be an issue here as we would do this for every cell. Though, I would assume that lodash's implementation is already quite optimized.

Would be happy to contribute a PR for this if you agree

Dejiah avatar Dec 11 '23 17:12 Dejiah

One work-around could be to create a custom TeamRow model with non-nested fields that you want to display.

hillstub avatar Feb 03 '24 16:02 hillstub

@hillstub Could you maybe provide a sample code? I am currently facing the same problem.

MMartin09 avatar Feb 17 '24 17:02 MMartin09