gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:46:23 26 4
gpt4 key购买 nike

我能够将数据插入 Book 表,但是当我尝试在该文档中显示数据时,它显示数据库错误。

我有一个疑问,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.

这是模型.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(我认为),但如果您不存储大量/实体,这就足够了。

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

关于python - 获取 "database error"(使用Django-nonrel的django、djangotoolbox、mongodbengine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29287613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com