-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
I have a pydantic model like this with a few issues preventing form rendering in the Admin UI:
class Binary(BaseModel):
model_config = ConfigDict(extra='ignore', populate_by_name=True)
name: BinName = 'binary-name' # I wish I could use None here / have no default, but that's also not supported
# provider_overrides: Dict[str, str] = Field(default={}, alias='overrides')
# ^ this also breaks, see separate issue: https://github.com/surenkov/django-pydantic-field/issues/64
loaded_provider: Optional[str] = Field(default=None, alias='provider')
loaded_abspath: Optional[str] = Field(default=None, alias='abspath')
# loaded_version: Optional[Tuple[int, int, int]] = Field(default=None, alias='version')
# this also doesn't work: Cannot read properties of undefined (reading 'hasOwnProperty')
When it gets rendered in the Admin UI it looks like this though:
I assume because the schema defines two allowed types string
and null
, so this becomes two "options" in the UI with a dropdown?
Potential solutions:
- If the only options are
<something>
andnull
(a very common case withOptional[]
fields), just show one field in the UI and treat leaving it empty asnull
- Generate better labels for the options dropdown based on the type, e.g. show
string
andnull
instead ofOption 1
andOption 2
- ⭐️ Show a radio selector to the left of the text field to choose the type, e.g.
string
/null
/int
etc. This would allow it to support moreUnion
types than justOptional
, as it could swap out the text field widget with widgets for other types depending on which radio button is clicked.
I like option 3 best as it's the most flexible for all Union types, but option 1 may be the least amount of code to implement to just get nullable types working fast.
Metadata
Metadata
Assignees
Labels
No labels