pymongo


flask-admin and pymongo search


I have the following ModelView
from flask.ext.admin.contrib.pymongo import ModelView
from flask.ext import login
from wtforms import form
class ImageView(ModelView):
column_labels = dict(
recordId='Record Id',
customerId='Customer Id',
createdAt='Created At',
updatedAt='Updated At',
imagePath='Image Path',
imageUrl='Image Url',
imageType='Image Type',
tag='Tag'
)
column_list = (
'_id',
'recordId',
'customerId',
'createdAt',
'updatedAt',
'imagePath',
'imageUrl',
'imageType',
'tag'
)
column_sortable_list = (
'customerId',
)
column_searchable_list = (
'customerId',
)
form = ImageForm
def is_accessible(self):
return login.current_user.is_authenticated()
def get_list(self, *args, **kwargs):
count, data = super(ImageView, self).get_list(*args, **kwargs)
return count, data
And I would like my customer id to be searchable, but unfortunately because it is an integer type in mongodb, the search will not work. How can I implement a custom search function to search that integer? Note that it is completely possible for my customerId to have duplicates because I am not enforcing uniqueness and that's my intent for this mongodb document.
Solved it. All I had to do really was to use custom_filters and apply my custom filter which casts the incoming filter value to an integer.
Like this:
class CustomFilterEqual(filters.BasePyMongoFilter):
"""
Custom filter which casts the filter value as an integer, if possible.
Failing which it will just set the value to an empty string, returning no results.
"""
def apply(self, query, value):
try:
value = int(value)
except:
value = ""
query.append({self.column: value})
print(query)
return query
def operation(self):
from flask.ext.admin.babel import gettext
return gettext('equals')
And in my ImageView class above, delete the column_searchable_list attribute and add in the column_filters attribute:
column_filters = (
CustomFilterEqual('customerId', 'Customer Id'),
)

Related Links

How do I deal with Error stated below?
pymongo query return in direct query not in pymongo
Displaying nested dictionaries in flask-admin
mongoengine slow serialization of embedded documents with reference fields
Pymongo error : 'Cursor' object has no attribute 'find' , how to filter it?
Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
How to enable `projection` in pymongo query class `find()`?
Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
Inserting string as regular string in mongodb
Mongos + Pymongo 2.5 ==>No suitable hosts found

Categories

HOME
gaming
seedstack
android-fragments
code-formatting
slider
google-play-services
mathematical-optimization
apache-kafka-connect
dji-sdk
capistrano
cocotb
port
getopenfilename
colors
pywinauto
pickle
significance
gitkraken
coreldraw
fluent-migrator
eclipse-emf
offline-caching
ruby-daemons
django-import-export
bobo-browse.net
nic
autodesk-model-derivative
winsock
vcenter
john-the-ripper
seafile-server
advanced-filter
landscape
twilio-php
llvm-ir
assemblies
best-buy-api
hdiv
fiware-wirecloud
common-table-expression
adblock
uiautomatorviewer
nusoap
email-injection
researchkit
rational-performance-test
java-2d
streamwriter
logical
jpda
data-management
clp
fiber
gulp-uglify
taco
okta-api
bjam
enaml
fantom
incron
mutators
htmltidy
sql-processor
encryption-symmetric
dnsjava
smacss
jquery-mobile-flipswitch
senchatouch-2.4
lsa
armv6
ggts
xsd2code
yii-booster
virtual-channel
indesign-server
.net-remoting
html-frames
mongo-jackson-mapper
chefspec
icenium
ckeditor.net
idispatch
struts2-json-plugin
story
getproperty
popup-blocker
telerik-ajax
first-class
uipangesturerecognizer
flymake
acpi
wiktionary
memory-pool
vows
anonymous-types
principles
iphone-sdk-4.3
asdoc
rawcontacts
subtext
ntruencrypt
icon-language

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App