FastUI icon indicating copy to clipboard operation
FastUI copied to clipboard

arrays in ModelForms - what can and can't be done atm?

Open pawrequest opened this issue 10 months ago • 2 comments

sorry if its not the place to ask, tell me if so

when trying to use arrays in modelforms the error is something like 'not fully supported'

what can and can't be done with arrays in modelforms? the error suggests something or other can?

i keep ending up making enums dynamically and it's overly complex so i revert to a normal form, but then the submit endpoint is super verbose because i have to list every field in the params.

i just want to set a bunch of options derrived from a list?

eg:

def make_address_enum(candidates: list[pf_ext.AddressRecipient]):
    return Enum(
        'AddressChoice',
        {f'address {i}': cand.address_line1 for i, cand in enumerate(candidates)}
    )

class BookingForm(BaseModel, ABC):
    address: Enum

def make_booking_form_type(candidates: list[pf_ext.AddressRecipient]) -> type[BaseModel]:
    class _Form(BookingForm):
        address: make_address_enum(candidates)

    return _Form

pawrequest avatar Apr 11 '24 20:04 pawrequest

You can create a form with dynamically built fields - you don't have to create it from a model. Something like this:

from fastui import FastUI, components as c
from fastui.forms import SelectOption


address_options = [SelectOption(value=address.id, label=address.line_1) for address in candidates]

booking_select_field = c.forms.FormFieldSelect(
    options=address_options,
    title='Select booking address',
    name='select_address',
    multiple=False)

booking_form = c.Form(form_fields=[booking_select_field], submit_url='/api/booking/create')

charlie-corus avatar Apr 14 '24 17:04 charlie-corus

yeah this is fine, but the modelform api makes it easier at the endpoint - i dont need to specify each field just a modeltype annotation.

is the 'not fully supported' actually a misnoma and the real answer is 'no array / sequence / list / tuple type fields are possible in modelform'?

if not, what can succesfully be achieved?

pawrequest avatar Apr 14 '24 18:04 pawrequest