获取 "database error"(使用 Django、djangotoolbox、Django-nonrel 的 mongodbengine)

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

我可以将数据插入图书 table,但是当我尝试在该文档中显示数据时,它显示数据库错误。

我有疑问,在 mongo 数据库中,多对多关系是否有效。

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.

这里是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

Django-nonrel 不支持多对多关系。

解决此问题的两个替代方法是。

首先,您可以像这样导入来使用 ListFields: 从 djangotoolbox.fields 导入 ListField

ListField 存储 ID 列表。 但是请注意,实体的数量限制为 1MB(我认为),但如果您不存储巨大的 volumes/entity.

,这就足够了

另一种选择是使用另一个实体来映射多对多关系。 这将消除上述限制,但存储和查询映射实体会产生开销。