title: Django Mongo SQLDecodeError at /admin/
body: <p>I have been experimenting with mongodb with Django. I created a super user, and then proceeded to <a href="http://localhost:8000/admin/" rel="nofollow noreferrer">http://localhost:8000/admin/</a> to login. Upon entering my credentials, and submitting them, the error given at the end of this post occurred. I have 2 other apps in this project and have also commented the line where the admin can register the model in <code>appname/admin.py</code>, i.e.:</p>
  
  <pre><code>#admin.site.register(Tweet)
  </code></pre>
  
  <p>How can I fix this?
  If you would like any further information about the code or from certain files please let me know and I will edit it in :)</p>
  
  <p>I assume the error is caused with the way mongo and django is workign together but have no idea how to correct it.</p>
  
  <p>I am sure the python installation, and routes all work correctly as they were configured with the assistance of the department admins. </p>
  
  <p><strong>NOTE:</strong> some of the routes are due to custom installation of Python in case it seems extremely weird. I am using a Department machine to do my work, hence the paths.</p>
  
  <p>Relevant Versions of modules/software:</p>
  
  <p>[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux</p>
  
  <ul>
  <li>python (3.6.4) </li>
  <li>Django (2.0.3) </li>
  <li>MongoDB shell version: (2.6.12) </li>
  <li>mongod --version results in: db version v2.6.12 </li>
  <li>pymongo (3.6.1) </li>
  <li>sqlparse (0.2.4) </li>
  <li>djongo (1.2.23)</li>
  </ul>
  
  <p>Db code in <code>settings.py</code>:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'djongo',
          'NAME': 'hamish-djongo-db',
          'USER': 'hamish_admin',
          'PASSWORD': 'thishasbeencensoredfortheinternet',
      }
  }
  </code></pre>
  
  <h2>Error in shell:</h2>
  
  <pre><code>Internal Server Error: /admin/
  Traceback (most recent call last):
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 454, in __iter__
      yield from iter(self._query)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 144, in __iter__
      self._cursor = self._get_cursor()
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 229, in _get_cursor
      cur = self.db_ref[self.left_table].aggregate(pipeline)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/collection.py", line 2185, in aggregate
      **kwargs)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/collection.py", line 2092, in _aggregate
      client=self.__database.client)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/pool.py", line 517, in command
      collation=collation)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/network.py", line 125, in command
      parse_write_concern_error=parse_write_concern_error)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/helpers.py", line 145, in _check_command_response
      raise OperationFailure(msg % errmsg, code, response)
  pymongo.errors.OperationFailure: exception: Unrecognized pipeline stage name: '$lookup'
  
  The above exception was the direct cause of the following exception:
  
  Traceback (most recent call last):
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
      response = get_response(request)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 158, in _get_response
      response = self.process_exception_by_middleware(e, request)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
      response = response.render()
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/response.py", line 106, in render
      self.content = self.rendered_content
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/response.py", line 83, in rendered_content
      content = template.render(context, self._request)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
      return self.template.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 175, in render
      return self._render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
      bit = node.render_annotated(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
      return self.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/loader_tags.py", line 155, in render
      return compiled_parent._render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
      bit = node.render_annotated(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
      return self.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/loader_tags.py", line 155, in render
      return compiled_parent._render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
      return self.nodelist.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
      bit = node.render_annotated(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
      return self.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/loader_tags.py", line 67, in render
      result = block.nodelist.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
      bit = node.render_annotated(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
      return self.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/defaulttags.py", line 314, in render
      return nodelist.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 943, in render
      bit = node.render_annotated(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
      return self.render(context)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/defaulttags.py", line 168, in render
      len_values = len(values)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/query.py", line 254, in __len__
      self._fetch_all()
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/query.py", line 62, in __iter__
      for row in compiler.results_iter(results):
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1460, in cursor_iter
      for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1460, in &lt;lambda&gt;
      for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/utils.py", line 96, in inner
      return func(*args, **kwargs)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/cursor.py", line 59, in fetchmany
      ret.append(self.result.next())
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 448, in __next__
      return next(self._result_generator)
    File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 466, in __iter__
      raise exe from e
  djongo.sql2mongo.SQLDecodeError: FAILED SQL: SELECT "django_admin_log"."id", "django_admin_log"."action_time", "django_admin_log"."user_id", "django_admin_log"."content_type_id", "django_admin_log"."object_id", "django_admin_log"."object_repr", "django_admin_log"."action_flag", "django_admin_log"."change_message", "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined", "django_content_type"."id", "django_content_type"."app_label", "django_content_type"."model" FROM "django_admin_log" INNER JOIN "auth_user" ON ("django_admin_log"."user_id" = "auth_user"."id") LEFT OUTER JOIN "django_content_type" ON ("django_admin_log"."content_type_id" = "django_content_type"."id") WHERE "django_admin_log"."user_id" = %(0)s ORDER BY "django_admin_log"."action_time" DESC LIMIT 10
  Pymongo error: OrderedDict([('errmsg', "exception: Unrecognized pipeline stage name: '$lookup'"), ('code', 16436), ('ok', 0.0)])
  Version: 1.2.23
  [29/Mar/2018 12:42:09] "GET /admin/ HTTP/1.1" 500 339290
  </code></pre>
  
  <h2>Error on page:</h2>
  
  <pre><code>Environment:
  
  
  Request Method: GET
  Request URL: http://localhost:8000/admin/
  
  Django Version: 2.0.3
  Python Version: 3.6.4
  Installed Applications:
  ['django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'widget_tweaks',
   'blogtest',
   'tweetish']
  Installed Middleware:
  ['django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware']
  
  
  Template error:
  In template /dcs/14/u1404526/.local/lib/python3.6/site-packages/django/contrib/admin/templates/admin/base.html, error at line 0
     FAILED SQL: SELECT "django_admin_log"."id", "django_admin_log"."action_time", "django_admin_log"."user_id", "django_admin_log"."content_type_id", "django_admin_log"."object_id", "django_admin_log"."object_repr", "django_admin_log"."action_flag", "django_admin_log"."change_message", "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined", "django_content_type"."id", "django_content_type"."app_label", "django_content_type"."model" FROM "django_admin_log" INNER JOIN "auth_user" ON ("django_admin_log"."user_id" = "auth_user"."id") LEFT OUTER JOIN "django_content_type" ON ("django_admin_log"."content_type_id" = "django_content_type"."id") WHERE "django_admin_log"."user_id" = %(0)s ORDER BY "django_admin_log"."action_time" DESC LIMIT 10
  Pymongo error: OrderedDict([('errmsg', "exception: Unrecognized pipeline stage name: '$lookup'"), ('code', 16436), ('ok', 0.0)])
  Version: 1.2.23
     1 : {% load i18n static %}&lt;!DOCTYPE html&gt;
     2 : {% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
     3 : &lt;html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}&gt;
     4 : &lt;head&gt;
     5 : &lt;title&gt;{% block title %}{% endblock %}&lt;/title&gt;
     6 : &lt;link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}" /&gt;
     7 : {% block extrastyle %}{% endblock %}
     8 : {% if LANGUAGE_BIDI %}&lt;link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}" /&gt;{% endif %}
     9 : {% block extrahead %}{% endblock %}
     10 : {% block responsive %}
  
  
  Traceback:
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py" in __iter__
    454.             yield from iter(self._query)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py" in __iter__
    144.             self._cursor = self._get_cursor()
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py" in _get_cursor
    229.             cur = self.db_ref[self.left_table].aggregate(pipeline)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/collection.py" in aggregate
    2185.                                    **kwargs)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/collection.py" in _aggregate
    2092.                 client=self.__database.client)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/pool.py" in command
    517.                            collation=collation)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/network.py" in command
    125.                 parse_write_concern_error=parse_write_concern_error)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/pymongo/helpers.py" in _check_command_response
    145.             raise OperationFailure(msg % errmsg, code, response)
  
  The above exception (exception: Unrecognized pipeline stage name: '$lookup') was the direct cause of the following exception:
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
    35.             response = get_response(request)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
    158.                 response = self.process_exception_by_middleware(e, request)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
    156.                 response = response.render()
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/response.py" in render
    106.             self.content = self.rendered_content
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/response.py" in rendered_content
    83.         content = template.render(context, self._request)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/backends/django.py" in render
    61.             return self.template.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render
    175.                     return self._render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in _render
    167.         return self.nodelist.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render
    943.                 bit = node.render_annotated(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
    910.             return self.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
    155.             return compiled_parent._render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in _render
    167.         return self.nodelist.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render
    943.                 bit = node.render_annotated(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
    910.             return self.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
    155.             return compiled_parent._render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in _render
    167.         return self.nodelist.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render
    943.                 bit = node.render_annotated(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
    910.             return self.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
    67.                 result = block.nodelist.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render
    943.                 bit = node.render_annotated(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
    910.             return self.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
    314.                 return nodelist.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render
    943.                 bit = node.render_annotated(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
    910.             return self.render(context)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
    168.             len_values = len(values)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/query.py" in __len__
    254.         self._fetch_all()
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/query.py" in _fetch_all
    1179.             self._result_cache = list(self._iterable_class(self))
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
    62.         for row in compiler.results_iter(results):
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in cursor_iter
    1460.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in &lt;lambda&gt;
    1460.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/django/db/utils.py" in inner
    96.                 return func(*args, **kwargs)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/cursor.py" in fetchmany
    59.                 ret.append(self.result.next())
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py" in __next__
    448.         return next(self._result_generator)
  
  File "/dcs/14/u1404526/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py" in __iter__
    466.             raise exe from e
  
  Exception Type: SQLDecodeError at /admin/
  Exception Value: FAILED SQL: SELECT "django_admin_log"."id", "django_admin_log"."action_time", "django_admin_log"."user_id", "django_admin_log"."content_type_id", "django_admin_log"."object_id", "django_admin_log"."object_repr", "django_admin_log"."action_flag", "django_admin_log"."change_message", "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined", "django_content_type"."id", "django_content_type"."app_label", "django_content_type"."model" FROM "django_admin_log" INNER JOIN "auth_user" ON ("django_admin_log"."user_id" = "auth_user"."id") LEFT OUTER JOIN "django_content_type" ON ("django_admin_log"."content_type_id" = "django_content_type"."id") WHERE "django_admin_log"."user_id" = %(0)s ORDER BY "django_admin_log"."action_time" DESC LIMIT 10
  Pymongo error: OrderedDict([('errmsg', "exception: Unrecognized pipeline stage name: '$lookup'"), ('code', 16436), ('ok', 0.0)])
  Version: 1.2.23
  </code></pre>