Search code examples
pythondjangomongodbdjango-admindjango-nonrel

getting "database error" (using django, djangotoolbox, mongodbengine of Django-nonrel)


I am able to insert data into Book table but when I try to show data inside that document it shows data base error.

I have a doubt, is manytomany relationship valid in mongo db.

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/books/book/55145ce436cc0d4b8f6308cd/

Django Version: 1.6.8
Python Version: 2.7.6
Installed Applications:
('django_mongodb_engine',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'books',
 'djangotoolbox')
Installed Middleware:
('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')


Traceback:
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  466.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
  371.                 return func(*args, **kwargs)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in change_view
  1271.             form = ModelForm(instance=obj)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/forms/models.py" in __init__
  315.             object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/forms/models.py" in model_to_dict
  141.                 data[f.name] = list(f.value_from_object(instance).values_list('pk', flat=True))
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  96.         self._fetch_all()
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  857.             self._result_cache = list(self.iterator())
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  1068.             for row in self.query.get_compiler(self.db).results_iter():
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/djangotoolbox-1.6.2-py2.7.egg/djangotoolbox/db/basecompiler.py" in results_iter
  375.             results = self.build_query(fields).fetch(
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/djangotoolbox-1.6.2-py2.7.egg/djangotoolbox/db/basecompiler.py" in build_query
  477.         self.check_query()
File "/home/nishank/djangoDoc2/venv/local/lib/python2.7/site-packages/djangotoolbox-1.6.2-py2.7.egg/djangotoolbox/db/basecompiler.py" in check_query
  455.             raise DatabaseError("This query is not supported by the database.")

Exception Type: DatabaseError at /admin/books/book/55145ce436cc0d4b8f6308cd/
Exception Value: This query is not supported by the database.

Here is model.py

`from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __unicode__(self):
        return self.name

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField(blank = True)

    def __unicode__ (self):
        return u'%s %s' %(self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title`

It is important to note that for mongodb support, followed this tutorial I installed django, djangotoolbox, mongodb-engine,( Django-nonrel ) Django-nonrel


Solution

  • Django-nonrel does not support many-to-may relationships.

    two alternative to work around this are.

    First you can use ListFields by importing it like so: from djangotoolbox.fields import ListField

    The ListField stores a list of IDs. Note however that the number of entities are limited to 1MB (I think) but this very adequate if you are not storing huge volumes/entity.

    Another alternative is to use another entity to map the many to many relationships. This will eliminate the above limitation but there will be an overhead in storing and querying the mapping entities.