Wednesday, February 13, 2013

Mongo, Django and the Admin View - work in progress

Once you switch Django to a MongDB backend with MongoEngine (see my previous posts), you loose Django's automatic admin view. This is a pity since it's such a nice web CRUD interface. There are some options though to get it back, among them mongoadmin Since I like working with Django 1.6 dev trunk, I needed to adapt mongoadmin in two spots: options.py and sites.py.

Here are the diffs of the changes I have made so far, without breaking backward compatibility, however surfing to the admin page still throws an error, so I will update this page when I get a chance and not commit (yet). Perhaps it's already useful for somebody - comments welcome!

For options.py
@@ -11,7 +11,11 @@
 from django.db import models, transaction, router
 from django.db.models.related import RelatedObject
 from django.db.models.fields import BLANK_CHOICE_DASH, FieldDoesNotExist
-from django.db.models.sql.constants import QUERY_TERMS, LOOKUP_SEP
+from django.db.models.sql.constants import QUERY_TERMS
+try:
+  from django.db.models.sql.constants import LOOKUP_SEP
+except ImportError:
+  from django.db.models.constants import LOOKUP_SEP
 from django.http import Http404, HttpResponse, HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render_to_response
 from django.utils.decorators import method_decorator

For sites.py
@@ -207,7 +207,10 @@
         return update_wrapper(inner, view)

     def get_urls(self):
-        from django.conf.urls.defaults import patterns, url, include
+        try:
+            from django.conf.urls.defaults import patterns, url, include
+        except ImportError:
+            from django.conf.urls import patterns, url, include

         if settings.DEBUG:
             self.check_dependencies()


BTW, I've started to use SyntaxHighlighter for code. Thanks Alex!

No comments: