Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions mongoadmin_project/mongoadmin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
url(r'^mongo/(?P<connection_name>[^/]+)/(?P<database_name>[^/]+)/$', views.DatabaseView.as_view()),
url(r'^mongo/(?P<connection_name>[^/]+)/(?P<database_name>[^/]+)/(?P<collection_name>[^/]+)/$', views.CollectionView.as_view()),
url(r'^mongo/(?P<connection_name>[^/]+)/(?P<database_name>[^/]+)/(?P<collection_name>[^/]+)/add/$', views.CreateDocumentView.as_view()),
url(r'^mongo/(?P<connection_name>[^/]+)/(?P<database_name>[^/]+)/(?P<collection_name>[^/]+)/(?P<pk>[a-z\d]+)/$', views.UpdateDocumentView.as_view()),
url(r'^mongo/(?P<connection_name>[^/]+)/(?P<database_name>[^/]+)/(?P<collection_name>[^/]+)/(?P<pk>[a-z\d]+)/delete/$', views.DeleteDocumentView.as_view()),
url(r'^mongo/(?P<connection_name>[^/]+)/(?P<database_name>[^/]+)/(?P<collection_name>[^/]+)/(?P<pk>[a-z\d\-]+)/$', views.UpdateDocumentView.as_view()),
url(r'^mongo/(?P<connection_name>[^/]+)/(?P<database_name>[^/]+)/(?P<collection_name>[^/]+)/(?P<pk>[a-z\d\-]+)/delete/$', views.DeleteDocumentView.as_view()),
)
6 changes: 5 additions & 1 deletion mongoadmin_project/mongoadmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ def prepare_document(document):

class BaseDocumentView(ConnectionDetailMixin):
def get_document(self):
document = self.collection.find_one({'_id': ObjectId(self.kwargs['pk'])})
try:
pk = ObjectId(self.kwargs['pk'])
except:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the specific exception that is raised?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need to convert pk into an ObjectId. PyMongo will do it for us. Quoted from "find_one" api description -

"Changed in version 1.7: Accept any type other than a dict instance as an "_id" query, not just ObjectId instances."

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, my bad! Actually PyMongo doesn't do it - Must have the right type to query.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flisky It doesn't work this way.

pk = self.kwargs['pk']
document = self.collection.find_one({'_id': pk})
return document

def get(self, request, *args, **kwargs):
Expand Down