Flask-admin: MongoEngine: filtering on ReferenceFields, EmailFields etc.

Created on 9 Dec 2013  路  8Comments  路  Source: flask-admin/flask-admin

I am trying to add column_filter for ReferenceFields and Email fields:

class UserView(ModelView):
    can_delete = False
    column_filters = ('email',)
admin.add_view(UserView(models.User))

but I get the error:

Exception: Unsupported filter type email

Am I missing something trivial or is it not supported yet?

Most helpful comment

Thanks! I was able to get it working by using the code below. I have a Tags db with a list of references to Artists.

class FilterArtists(BaseMongoEngineFilter):
    def apply(self, query, value):
        return query.filter(artists__in=models.Artist.objects(name__icontains=value))

    def operation(self):
        return gettext('contains')

class FilterArtistsNot(BaseMongoEngineFilter):
    def apply(self, query, value):
        return query.filter(artists__nin=models.Artist.objects(name__icontains=value))

    def operation(self):
        return gettext('doesn\'t contain')

and setup the column filter in the Tag ModelView like so:

    column_filters = [
        FilterLowercase('name', 'Name'),
        FilterLowercaseNot('name', 'Name'),
        FilterArtists('artists', 'Artist Name'),
        FilterArtistsNot('artists', 'Artist Name'),
        ]

All 8 comments

Filter by reference does not make any sense - you need to filter by concrete field.

Filter by email was not supported, I added it to the list.

class First(db.Document):
    name = db.StringField()

class Second(db.Document):
    number = db.IntField()
    first = db.ReferenceField(First)

Lets consider the above dummy schema created using mongoengine. Now I want to be able to filter the fields of Second model using the name attribute of the First model. How may I achieve this?

@bhanuvrat did you ever figure this out? i'm needing to do something similar.

This is not possible in MongoEngine and Flask-Admin, as a result. It is not possible to access related model through ReferenceField when querying. For more information, see here: http://stackoverflow.com/questions/6570432/querying-referencefields-with-mongoengine

While Flask-Admin does not support this functionality right away, you can build custom filter that makes query against First collection and returns ID to use as a filter value for Second query. For some examples, see here: https://github.com/mrjoes/flask-admin/blob/master/flask_admin/contrib/mongoengine/filters.py

Thanks! I was able to get it working by using the code below. I have a Tags db with a list of references to Artists.

class FilterArtists(BaseMongoEngineFilter):
    def apply(self, query, value):
        return query.filter(artists__in=models.Artist.objects(name__icontains=value))

    def operation(self):
        return gettext('contains')

class FilterArtistsNot(BaseMongoEngineFilter):
    def apply(self, query, value):
        return query.filter(artists__nin=models.Artist.objects(name__icontains=value))

    def operation(self):
        return gettext('doesn\'t contain')

and setup the column filter in the Tag ModelView like so:

    column_filters = [
        FilterLowercase('name', 'Name'),
        FilterLowercaseNot('name', 'Name'),
        FilterArtists('artists', 'Artist Name'),
        FilterArtistsNot('artists', 'Artist Name'),
        ]

@gordol No I hadn't. Thanks for the solution, will try it out in my app as well.

@gordol Your Solution helped !! Thanks

Was this page helpful?
0 / 5 - 0 ratings