Skip to content

Update deprecated syntax of pymongo #1993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions examples/pymongo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from bson.objectid import ObjectId

from flask import Flask
import flask_admin as admin
from flask_admin import Admin

from wtforms import form, fields

Expand All @@ -16,11 +16,10 @@
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'

# Create models
conn = pymongo.Connection()
# Connection to mongodb
uri = "Paste connection string"
db = conn.test


# User admin
class InnerForm(form.Form):
name = fields.StringField('Name')
Expand Down Expand Up @@ -74,7 +73,7 @@ def get_list(self, *args, **kwargs):

# Grab user names
query = {'_id': {'$in': [x['user_id'] for x in data]}}
users = db.user.find(query, fields=('name',))
users = db.user.find(query, {'name': 1})

# Contribute user names to the models
users_map = dict((x['_id'], x['name']) for x in users)
Expand All @@ -86,7 +85,7 @@ def get_list(self, *args, **kwargs):

# Contribute list of user choices to the forms
def _feed_user_choices(self, form):
users = db.user.find(fields=('name',))
users = db.user.find({}, {'name': 1})
form.user_id.choices = [(str(x['_id']), x['name']) for x in users]
return form

Expand All @@ -99,7 +98,7 @@ def edit_form(self, obj):
return self._feed_user_choices(form)

# Correct user_id reference before saving
def on_model_change(self, form, model):
def on_model_change(self, form, model, is_created):
user_id = model.get('user_id')
model['user_id'] = ObjectId(user_id)

Expand All @@ -114,7 +113,7 @@ def index():

if __name__ == '__main__':
# Create admin
admin = admin.Admin(app, name='Example: PyMongo')
admin = Admin(app, name='Example: PyMongo')

# Add views
admin.add_view(UserView(db.user, 'User'))
Expand Down