Flask-admin: Tip: How to populate Filter options from database

Created on 27 May 2017  路  3Comments  路  Source: flask-admin/flask-admin

(For my future reference and will hopefully help someone else)

The docs are pretty clear on adding custom filter with pre-defined options: http://flask-admin.readthedocs.io/en/latest/api/mod_contrib_mongoengine/
But it was unclear to me how to populate the options from database. Here is solution I found through trial and error. If there is a better way, direction is welcome.

Example (I'm using MongoEngine):

# Showing relevant code

# models
class BaseballTeam:
    team = db.StringField()

class BaseballCard(db.Document):
    player = db.StringField()
    team = db.ReferenceField(BaseballTeam)

# My primary challenge learning when/where to make call to database
def _get_teams(self):
        opts = [(str(x.id), x.team) for x in BaseballTeam.objects().all()]
        return opts

class FilterTeam(BaseMongoEngineFilter):
    def apply(self, query, value):
        return query.filter(team__contains=value)

    # Turns out, there's a handy method for populating the options
    def get_options(self, view):
        return _get_teams(self)

    def operation(self):
        return 'contains'


class BaseballCardAdmin(ModelView):
    ...
    column_filters = [ FilterTeam(
                                  column='team', name='Team',
                                  #options=()  #not sure if it's possible to call '_get_teams()' from here ?
                                )
                              ]

   ...

Most helpful comment

Hi, you can uggly patch for force reload cache.

column_filters =  ( filters.FilterEqual('team', 'Team', options=_get_teams), )

def get_list(self, *args, **kwargs):    
       self._refresh_filters_cache() # Ugly Force update filter and options cache
       count, data = super(self.__class__, self).get_list(*args, **kwargs)
       return count, data

All 3 comments

very productive example and please change title to flask admin custom filter because current title is not as informational as it does the work 馃憤

@macfire can you tell me how to filter embedded field ?

class SpecialPrice(EmbeddedDocument):
    value = FloatField()
    is_active = BooleanField(default=False)
    active_from = DateTimeField()
    active_to = DateTimeField()

    def __unicode__(self):
      return self.value

class Product(Document):
..
    special_price = EmbeddedDocumentField(SpecialPrice)
..

Hi, you can uggly patch for force reload cache.

column_filters =  ( filters.FilterEqual('team', 'Team', options=_get_teams), )

def get_list(self, *args, **kwargs):    
       self._refresh_filters_cache() # Ugly Force update filter and options cache
       count, data = super(self.__class__, self).get_list(*args, **kwargs)
       return count, data
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DandyDev picture DandyDev  路  7Comments

macfire picture macfire  路  6Comments

galuszkak picture galuszkak  路  3Comments

timgogochen picture timgogochen  路  7Comments

AhnSeongHyun picture AhnSeongHyun  路  4Comments