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

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

Open
pawrequest opened this issue Apr 11, 2024 · 2 comments
Open

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

pawrequest opened this issue Apr 11, 2024 · 2 comments

Comments

@pawrequest
Copy link

pawrequest commented Apr 11, 2024

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
@charlie-corus
Copy link

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')

@pawrequest
Copy link
Author

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?

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

No branches or pull requests

2 participants