Search code examples
flaskflask-sqlalchemyflask-admin

How can we NOT display certain rows in a flask-admin view?



I have been searching and trying for a while but could not find any answer so any help is appreciated.
I have a flask admin-view, say the following:
class testViewMain():
    column_list = [
        'first_name',
        'last_name',
        'email',
        'phone',
    ]

How can I exclude certain values on this flask view?
For example, display only rows from the database where first_name != John and last_name != Doe.

I thought the function I was looking for is on_form_prefill, but according to the documentation, this is only on the edit view.


Solution

  • I found an answer:

    class testViewMain():
        column_list = [
            'first_name',
            'last_name',
            'email',
            'phone',
        ]
    
        def get_query(self):
           return self.session.query(self.model).filter(
                           self.model.first_name != 'John' \
                       and self.model.last_name != 'Doe')