pythonsqlalchemyflask-appbuilder

Flask-AppBuilder how to set primary key on create?


My users should be able to create new rows for my table, but when I use the plus-button in Flask-AppBuilder, the primary key is not shown and the generated SQL INSERT statement is missing the primary key, which obviously fails.

How can I get Flask-AppBuilder to show the primary key for new rows?

Example

models.py

class Catalogue(Model):                                                                                                                                                                                                                                                                                                       
    id = Column(String(200), primary_key=True)
    label =  Column(String(200), nullable=False)
    type =  Column(Enum("UserGroup","ApplicationSystem","Feature","EnterpriseFunction","OrganizationalUnit"), nullable=False)

views.py

class CatalogueView(ModelView):
    datamodel = SQLAInterface(Catalogue)
    label_columns = {'label':'Name', }
    list_columns = ['id', 'label', 'type']
    related_views = [ClassifiedView]

Now when I run the application, I can see the "id" field in the view http://127.0.0.1:5000/catalogueview/list/.

However when I enter the detail view at http://127.0.0.1:5000/catalogueview/show/myexamplecatalogue, then the primary key field "id" is hidden and the same happens when creating a new entry using http://127.0.0.1:5000/catalogueview/add, which then fails as stated above.

How can I stop Flask-AppBuilder from hiding my primary key and successfully create new entries?


Solution

  • As suggested by @IljaEverilä, this can be added using the "edit_columns" attribute:

    class CatalogueView(ModelView):
      datamodel = SQLAInterface(Catalogue)
      label_columns = {'label':'Name', }
      list_columns = ['id', 'label', 'type']
      related_views = [ClassifiedView]
      edit_columns = ['id', 'label', 'type']