How to add admin using flask_pymongo and flask_admin

Viewed 1862

How to add admin using flask_pymongo and flask_admin

```

from flask import Flask
from flask_pymongo import PyMongo

from bson import json_util

from bson.objectid import ObjectId
import flask_admin as admin

from wtforms import form, fields

from flask_admin.form import Select2Widget
from flask_admin.contrib.pymongo import ModelView, filters
from flask_admin.model.fields import InlineFormField, InlineFieldList
from flask_pymongo import PyMongo

# app settings
app = Flask(__name__)

app.config['MONGO_URI'] = 'mongodb://username:passwrod@ds156811.mlab.com:56811/somedb'
app.config['MONGO_CONNECT'] = True
app.secret_key = 'secret'

mongo = PyMongo(app)
db = mongo


class UserForm(form.Form):
    name = fields.StringField('Name')
    email = fields.StringField('Email')

class UserView(ModelView):
    column_list = ('name', 'email')
    form = UserForm

admin = admin.Admin(app)


admin.add_view(UserView(db['users']))

```

Here is small example taken from https://flask-admin.readthedocs.io/en/v1.0.9/db_pymongo/ Here is another: https://github.com/flask-admin/flask-admin/tree/master/examples/pymongo

The flask-admin community has used pymongo but I want to use flask-pymongo package.

I am okay with most of the code but adding of view to the admin page is creating problem, admin.add_view() function is creating problem.

The question is how to add view to admin page, by taking flask-admin and flask-pymongo(pymongo is different)?

Edited: error message:

TypeError: 'PyMongo' object is not subscriptable

The object we get using the pymongo is different from what we get from pyflask-mongo, which is being responsible for the problem.

2 Answers
Related