Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Get Started with Django MongoDB Extensions

In this tutorial, you can learn how to use the Django MongoDB Extensions package in your Django project.

Django MongoDB Extensions is a package that extends Django MongoDB Backend with developer tooling. This tutorial shows you how to configure the MongoDB Query Language (MQL) panel in Django MongoDB Extensions to use with the Django Debug Toolbar. This panel shows the MQL queries that your application runs during a request, along with execution times and query plans.

Before you begin this tutorial, complete the following prerequisite tasks:

  • Create a Django project that uses Django MongoDB Backend. To create a project, see the Get Started with Django MongoDB Backend tutorial.

  • Install and configure the Django Debug Toolbar in your project. To view instructions, see Installation in the Django Debug Toolbar documentation.

The following steps show you how to install Django MongoDB Extensions, configure the MQL panel, and view MongoDB queries in the Django Debug Toolbar.

1

Run the following command to install Django MongoDB Extensions:

pip install django-mongodb-extensions
2

Navigate to your project's settings.py file and add "django_mongodb_extensions" to the INSTALLED_APPS setting, as shown in the following example:

INSTALLED_APPS = [
'django_mongodb_extensions',
# ... your other apps
]
3

By default, the Django Debug Toolbar works without requiring a DEBUG_TOOLBAR_PANELS setting. However, because MongoDB doesn't use SQL, the most common configuration for MongoDB applications is to replace the SQL panel with the MQL panel. Add the following setting to your settings.py file, which sets DEBUG_TOOLBAR_PANELS to the default values but replaces SQLPanel with MQLPanel:

DEBUG_TOOLBAR_PANELS = [
'debug_toolbar.panels.history.HistoryPanel',
'debug_toolbar.panels.versions.VersionsPanel',
'debug_toolbar.panels.timer.TimerPanel',
'debug_toolbar.panels.settings.SettingsPanel',
'debug_toolbar.panels.headers.HeadersPanel',
'debug_toolbar.panels.request.RequestPanel',
'django_mongodb_extensions.mql_panel.MQLPanel',
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
'debug_toolbar.panels.templates.TemplatesPanel',
'debug_toolbar.panels.alerts.AlertsPanel',
'debug_toolbar.panels.cache.CachePanel',
'debug_toolbar.panels.signals.SignalsPanel',
'debug_toolbar.panels.community.CommunityPanel',
'debug_toolbar.panels.redirects.RedirectsPanel',
'debug_toolbar.panels.profiling.ProfilingPanel',
]

Tip

Optional Settings

To learn how to set optional settings that customize the MQL panel behavior, see the README in the Django MongoDB Extensions repository on GitHub.

4

Start your development server by running the following command:

python manage.py runserver

Navigate to any page in your application that queries MongoDB. Open the Django Debug Toolbar and select the MQL panel to view the MongoDB queries executed during the request, including their execution times and explain plans.

5

If you configure Django MongoDB Extensions for the application created in the Get Started with Django MongoDB Backend tutorial, you can navigate to http://127.0.0.1:8000/recent_movies/ to view MongoDB queries executed during the request.

The Django Debug Toolbar appears at the right side of the page. Select the MQL label to view the MongoDB query used to display the recent movies, which resembles the following code:

db.movies.aggregate([{'$addFields': {'released': '$released'}}, {'$sort': SON([('released', -1)])}, {'$limit': 5}])

Then, you can explore the query information by clicking on the following buttons:

  1. Select the + button to the left of the query to view the traceback, which resembles the following code:

    /path/to/site-packages/django/contrib/staticfiles/handlers.py in __call__(80)
    return self.application(environ, start_response)
    /path/to/djangoquickstart/sample_mflix/views.py in recent_movies(11)
    return render(request, "recent_movies.html", {"movies": movies})
    /path/to/site-packages/django/shortcuts.py in render(25)
    content = loader.render_to_string(template_name, context, request, using=using)
    ...
  2. Select the Query button to the right of the query, in the ACTION column, to view the pretty-printed MQL. This MQL resembles the following code:

    db.movies.aggregate(
    [
    {
    "$addFields": {
    "released": "$released"
    }
    },
    {
    "$sort": {
    "released": -1
    }
    },
    {
    "$limit": 5
    }
    ]
    )
  3. Select the Explain button to the right of the query, in the ACTION column, to view the explain plan for the query. This plan resembles the following code:

    {
    "explainVersion": "1",
    "stages": [
    {
    "$cursor": {
    "queryPlanner": {
    ...
    },
    "executionStats": {
    ...
    }
    },
    "nReturned": 21351,
    "executionTimeMillisEstimate": 59
    },
    {
    "$addFields": {
    "released": "$released"
    },
    "nReturned": 21351,
    "executionTimeMillisEstimate": 70
    },
    {
    "$sort": {
    "sortKey": {
    "released": -1
    },
    "limit": 5
    },
    ...
    }
    ],
    ...
    }

Congratulations on completing the Django MongoDB Extensions tutorial! You now have a Django application that uses Django MongoDB Extensions to view and analyze MongoDB queries in the Django Debug Toolbar.

To learn more about the Django MongoDB Extensions package, see django-mongodb-extensions on PyPI and the django-mongodb-extensions repository on GitHub.

To learn how to query your data, see the Specify a Query guide.

Back

Queryable Encryption

On this page