Flask-Admin: how to show all fields

3.4k views Asked by At

Well I have different collections (i.e. tables), and they have different columns, say collection A has cols a1 and a2, collection B has b1 and b2. I want to show all columns

tried following

class UserView(ModelView):
    column_list = None

also

column_exclude_list = None

getting

NotImplementedError

how can I display all fields?

I mean

column_list = ('a1','a2','b1','b2') 

works, but I do not want to see empty b1 and b2 fields when looking collection A.

1

There are 1 answers

4
Mensur On

In Flask-Admin one class inheriting ModelView normally represents one SQLAlchemy model. You can show columns of multiple tables in one view if they are connected via relationship or hybrid property. ModelView serves primarily to add basic CRUD operations to your models.

So in your case your should have two classes and two views

class AView(ModelView):
    column_list('a1', 'a2')      #also if you don't add the column list           
                                 #property, all columns except primary key will be shown

class BView(ModelView):
    column_list('b1','b2')


app.add_view(AView(db.session, category='Models'))
app.add_view(BView(db.session, category='Models'))

See this example: http://examples.flask-admin.org/sqla/simple/admin/

Hope this helps.

In case you don't want to explicitly list all columns you define the constructor as following and all columns in the table (except PK) will be shown

class AView(ModelView):

    def __init__(self,session, **kwargs):
        super(AView, self).__init__(ModelName,session,**kwargs) 

You replace the ModelName with the name of your model class.