title: Flask-Admin with mongoengine - how to set a “persistent filter” on a view?
body: <p>I try to use Flask-Admin with Mongoengine and I am totally stuck. I can’t find a solution to implement a “persistent filter”. What I mean by that is, a user should only see and interact with items in a view (list), that are resulting from a custom query in mongoengine.</p>
  
  <p>If I have a model</p>
  
  <pre><code>class User(Document):
      name = StringField()
      days = DecimalField()
      book = StringField()
  </code></pre>
  
  <p>and I want to show in the first view only books depending on a specific user, I would do a query:</p>
  
  <pre><code>user_peter = User.objects(name=’Peter’)
  </code></pre>
  
  <p>and if I want to show overdue books for Peter, I would do a query:</p>
  
  <pre><code>overdue_books = User.objects(name=’Peter’, days__gt=28)
  </code></pre>
  
  <p>how can I implement that in a custom flask-admin-view? What do I have to set in the ModelView?</p>
  
  <pre><code>Class LentBooks(ModelView):
      menu_class_name=”Your Books”
      #???
  
  Class OverdueBooks(ModelView):
      menu_class_name=”Overdue Books”
      #???
  </code></pre>
  
  <p>or do I have to put it here?</p>
  
  <pre><code>Admin.add_view(LentBooks(model=User, #???))
  </code></pre>
  
  <p>For SQLAlchemy, there are many recipes, but not for mongoengine. Please help!</p>
  
  <p>What I have found, is in flask_admin.contrib.mongoengine.filters</p>
  
  <pre><code>class FilterEqual(BaseMongoEngineFilter):
      def apply(self, query, value):
          flt = {'%s' % self.column.name: value}
          return query.filter(**flt)
  
      def operation(self):
          return lazy_gettext('equals')
  </code></pre>
  
  <p>But how to implement this??</p>
  
  <p><strong>THANK YOU pjcunningham!!</strong></p>
  
  <p>Here is the solution from <strong>pjcunningham</strong> in a small test-source:</p>
  
  <pre><code>from flask import Flask
  import flask_admin as admin
  from flask_mongoengine import MongoEngine
  from flask_admin.contrib.mongoengine import ModelView
  
  app = Flask(__name__)
  
  app.config['SECRET_KEY'] = '1234567890'
  app.config['MONGODB_SETTINGS'] = {'db': 'library'}
  
  db = MongoEngine()
  db.init_app(app)
  
  class User(db.Document):
      name = db.StringField()
      days = db.DecimalField()
      book = db.StringField()
  
  class OverdueBooks(ModelView):
      def get_query(self):
          overdue_books = User.objects(name='Peter', days__gt=28)
          return overdue_books
  
  if __name__ == '__main__':
  
      admin = admin.Admin(app, 'Example: Library')
  
      admin.add_view(OverdueBooks(model=User, name='Library'))
  
      app.run(debug=True)
  </code></pre>
  
  <p>in MongoDB:</p>
  
  <pre><code>{ "_id" : ObjectId("5815f403e198515188860e3c"), "name" : "Jane", "days" : 21, "book" : "Testbook1" }
  { "_id" : ObjectId("5815f416e198515188860e3d"), "name" : "Peter", "days" : 30, "book" : "Testbook2" }
  </code></pre>
  
  <p><strong>Thank you very much! It works!</strong></p>