pydantic / FastUI

Build better UIs faster.
https://fastui-demo.onrender.com
MIT License
8.09k stars 307 forks source link

Nested models #86

Open wpeterw opened 9 months ago

wpeterw commented 9 months ago

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
samuelcolvin commented 9 months ago

You mean in a table?

I think that's not yet supported.

wpeterw commented 9 months ago

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

Dejiah commented 9 months ago

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

hillstub commented 7 months ago

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

MMartin09 commented 7 months ago

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