Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support finding initial value from a list of values #161

Merged
merged 2 commits into from
Feb 8, 2024
Merged

fix: support finding initial value from a list of values #161

merged 2 commits into from
Feb 8, 2024

Conversation

EspenAlbert
Copy link
Contributor

Hi! Awesome job on yet another repository! 👏

Before this
passing a list to ModelForm(initial={"some_field": SOME_LIST}) didn't work.
See #91

Mini example (not compilable):

from fastui import components as c
from pydantic import BaseModel


@router.get("/some-api-path",  response_model=FastUI,  response_model_exclude_none=True)
def some_route() -> list[c.AnyComponent]:
  return [c.ModelForm(
    model=SelectForm,
    submit_url="./some-submit-url",
    initial={"tools": [MyEnum.stick]},
  )]


class MyEnum(str, Enum):
  hammer = "hammer"
  stick = "stick"


class SelectForm(BaseModel):
  tools: list[MyEnum] = Field(default_factory=list)

Copy link

codecov bot commented Jan 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (cec25c6) 94.05% compared to head (279e464) 94.05%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #161   +/-   ##
=======================================
  Coverage   94.05%   94.05%           
=======================================
  Files          11       11           
  Lines         723      723           
=======================================
  Hits          680      680           
  Misses         43       43           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samuelcolvin
Copy link
Member

Thanks so much.

Code looks fine, but why would you want the initial value to be a list? Should your example have multiple=True in it?

Also, does this fix #91? I'm not clear if it does.

@EspenAlbert
Copy link
Contributor Author

Yes, this fixes #91 . I got a bit confused by the description of that issue and tried the solution of using value prop instead of defaultValue, but that wasn't fixing it.

Not sure where you mean the multiple=True should be? 😅

I think the multiple=True is not required since the SelectForm.tools is a list[Enum] and not Enum, and the ModelForm infers that 🤔

@samuelcolvin samuelcolvin merged commit 5830432 into pydantic:main Feb 8, 2024
16 checks passed
@samuelcolvin
Copy link
Member

makes sense, thanks so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants