This is a minor question, but I'm curious if there is something I missed...
I use a chameleon partial in a template via render_partial('partial.pt', row=row), where the template is using ${ row.item }.
When I use the same template in an endpoint, I need to wrap my view model in a dictionary like: chameleon_partials.extend_model({'card': view_model.to_dict()})
Is there a cleaner way I have missed, or am I just outside the intended usage? ;-)
I'm writing a fastAPI application using chameleon_partials and fastapi-chameleon together with htmx and tailwindcss. This makes a great combination since my "partials" can become self contained components.
Click for some more details...
Basically I have a tab, which shows a number of cards, each card has a number of rows. Each row offer inline editing using htmx [Click To Edit](https://htmx.org/examples/click-to-edit/).
In my code I fetch the content of the tab, which returns a number of cards, with the following endpoint:
```
@router.get('/tab/active')
@template(template_file='home/active_tab.pt')
async def index_active_tab(request: Request) -> fastapi.responses.HTMLResponse:
card_filter = CardFilter(date=datetime.date.today())
vm = ActiveCardsViewModel(request, card_filter)
await vm.load()
return chameleon_partials.extend_model(vm.to_dict())
```
The chameleon template for that tab includes a number of cards using:
```
```
Now I have some htmx code that saves an edited row, and replaces the updated card:
```
fastapi.responses.HTMLResponse:
row = await service.update_row_by_id(row_id=row_id,...)
vm = CardViewModel(request)
await vm.load_from_row_id(row_id)
card = {'card': vm.to_dict()} # This extra step makes me wonder if I'm doing something wrong...
return chameleon_partials.extend_model(card)
```
You probably, recognize the code style, it's stolen from a great course... ;-)
I rewrote the view model to include a dataclass object (i.e. the CardViewModel got a card attribute containing a Card data class) and thus got the expected hierarchy level. :-)
This is a minor question, but I'm curious if there is something I missed...
I use a chameleon partial in a template via
render_partial('partial.pt', row=row)
, where the template is using${ row.item }
. When I use the same template in an endpoint, I need to wrap my view model in a dictionary like:chameleon_partials.extend_model({'card': view_model.to_dict()})
Is there a cleaner way I have missed, or am I just outside the intended usage? ;-)
I'm writing a fastAPI application using chameleon_partials and fastapi-chameleon together with htmx and tailwindcss. This makes a great combination since my "partials" can become self contained components.
Click for some more details...
Basically I have a tab, which shows a number of cards, each card has a number of rows. Each row offer inline editing using htmx [Click To Edit](https://htmx.org/examples/click-to-edit/). In my code I fetch the content of the tab, which returns a number of cards, with the following endpoint: ``` @router.get('/tab/active') @template(template_file='home/active_tab.pt') async def index_active_tab(request: Request) -> fastapi.responses.HTMLResponse: card_filter = CardFilter(date=datetime.date.today()) vm = ActiveCardsViewModel(request, card_filter) await vm.load() return chameleon_partials.extend_model(vm.to_dict()) ``` The chameleon template for that tab includes a number of cards using: ```Thanks for the chameleon packages!
I rewrote the view model to include a
dataclass
object (i.e. theCardViewModel
got acard
attribute containing aCard
data class) and thus got the expected hierarchy level. :-)All is well.