0

I want to extend one field from add form, to be able to choose option from select list got by external API. Let me explain the question. I've got model like this:

class ContactGroup(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=True, nullable=False)
    my_field_id = Column(Integer, nullable=False)

    def __repr__(self):
        return self.name

When I want to add new entity, by default form created by app-builder, I have to specify my_field_id "by hand". So my question is, can I extend my_field_id to instead of specifying integer I will have a select list with data returned by external API. Api is providing data as a json, ex.:

{
  data: [
    {
      id: 1,
      name: "My field"
    }
    ...
  ]
}

Or is it too complicated for default app-builder forms and I have to rewrite add form completely?

Thanks!

1 Answers1

0

I know that's late answer but, if anyone will ever wants to do something similar, then SelectField is an answer. You can specify select options in init method like this:

class ContactGroup(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=True, nullable=False)
    my_field_id = SelectField(
        "Legal entity",
        widget=Select2Widget(),
        description="Select a legal entity",
        validators=[validators.Optional()],
        coerce=int,
    )

    def my_field_id_query():
        # get data here...
    
    def __init__:
        super().__init__(*args, **kwargs)
        self.my_field_id.choices = [(choice["id"], choice["name"]) for choice in my_field_id_query()] 

More detailed explanation here:

https://github.com/dpgaspar/Flask-AppBuilder/issues/1717