Dronakurl / nicecrud

Edit your pydantic models with a nice GUI - CRUD Create Update Replace Delete
MIT License
20 stars 4 forks source link

Support for lists of models #2

Closed RalfG closed 3 months ago

RalfG commented 3 months ago

Thanks for the great NiceGUI extension!

Would it be possible to support lists of models? Currently, this results in an ERROR value in the GUI field.

For example:

import logging

from nicegui import ui
from pydantic import BaseModel, SerializationInfo, model_serializer, field_serializer

from niceguicrud import NiceCRUD, NiceCRUDConfig

log = logging.getLogger("nicecrud")
log.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter("%(levelname)s - %(name)s - %(message)s"))
log.addHandler(console_handler)

class MovieModel(BaseModel):
    title: str
    year: int

    @model_serializer(mode="wrap")
    def gui(self, default_serializer, info: SerializationInfo):
        context = info.context  # pyright: ignore[reportAttributeAccessIssue]
        if context and context.get("gui"):
            return self.title
        return default_serializer(self)

class Actor(BaseModel):
    name: str
    age: int
    movies: list[MovieModel] = []

    @field_serializer("movies")
    def movies_show(self, v: list[MovieModel], info: SerializationInfo):
        context = info.context
        if context and context.get("gui"):
            return ", ".join([m.title for m in v])
        return v

actress = Actor(
    name="Scarlett Johansson",
    age=36,
    movies=[
        MovieModel(title="Lost in Translation", year=2003),
        MovieModel(title="Lucy", year=2014),
    ],
)
another_actress = Actor(
    name="Natalie Portman",
    age=40,
    movies=[
        MovieModel(title="Black Swan", year=2010),
        MovieModel(title="V for Vendetta", year=2005),
    ],
)
crud_config = NiceCRUDConfig(id_field="name", heading="Actress Database")
crud_app = NiceCRUD(basemodels=[actress, another_actress], config=crud_config)

ui.run()

This works in the card layout:

image

But the movies model list cannot be edited in the GUI:

image

Could support for this be easily added?

Thanks!

RalfG commented 3 months ago

Fixed in #3