Tastypie 入门:没有这样的 table:myapp_entry
Getting Started with Tastypie: no such table: myapp_entry
正如页面标题所说,我 Getting Started with Tastypie 按照链接的教程进行操作。但是当我尝试加载 /api/entry/?format=json
时,我收到 HTTP 500 响应,并显示以下错误消息:
没有这样的 table: myapp_entry
当我查看sqlite3时,确实没有table。
以下是我学习教程的方法:
$ django-admin startproject mysite
$ cd mysite
$ django-admin startapp myapp
我created/edited myapp/models.py
, myapp/api.py
和 mysite/urls.py
按照教程中的说明,并在[中将 'tastypie' 添加到我的 INSTALLED_APPS
=19=].
注意:我不清楚要编辑或创建哪个 urls.py
文件,所以我编辑了mysite
中已有一个。所以现在它看起来像这样:
from django.conf.urls import url, include
from myapp.api import EntryResource
from django.contrib import admin
entry_resource = EntryResource()
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^blog/', include('myapp.urls')),
url(r'^api/', include(entry_resource.urls)),
]
我注释掉了 'blog' 行,因为它导致了错误 ImportError: No module named 'myapp.urls'。我认为这是我正在填充的步骤,但是当我尝试将教程代码放入 myapp/urls.py
时,我在尝试加载页面时收到了 404,然后我尝试添加 url(r'^blog/', include('myapp.urls'))
到 mysite/urls.py
,我得到了堆栈溢出。所以我回到上面显示的代码。
明确地说,这是我的文件结构现在的样子:
$ find . -type f -not -name '*.pyc'
./manage.py
./myapp/__init__.py
./myapp/views.py
./myapp/models.py
./myapp/tests.py
./myapp/admin.py
./myapp/apps.py
./myapp/migrations/__init__.py
./myapp/api.py
./db.sqlite3
./mysite/__init__.py
./mysite/settings.py
./mysite/urls.py
./mysite/wsgi.py
我做的另一个改变是在我的 Entry
class 中添加一个 Meta
subclass,所以前十几行看起来像这样:
class Entry(models.Model):
user = models.ForeignKey(User)
pub_date = models.DateTimeField(default=now)
title = models.CharField(max_length=200)
slug = models.SlugField(null=True, blank=True)
body = models.TextField()
class Meta:
app_label = 'myapp'
# __unicode__() and save() as in the tutorial
如果我不这样做,我会在控制台中得到这个:RuntimeError: Model class myapp.models.Entry doesn't declare explicit app_label and不在 INSTALLED_APPS.
的应用程序中
我尝试的另一件事是 python manage.py migrate
。它找到了要做的事情,但这并没有解决这个错误。
这是我安装的(在 requirements.txt
/virtualenv
中):
- Django (1.10)
- django-tastypie (0.13.3)
- PyYAML (3.12)
- mysqlclient (1.3.10) (虽然我还没有真正设置它——我现在使用默认的sqlite3设置)
我是 运行 Python 3.4.3,但是我在 Python 2.7.6.
中使用非常相似的设置得到了完全相同的错误
最后,这是来自该 HTTP 500 页面的完整堆栈跟踪:
{"error_message": "no such table: myapp_entry", "traceback": "Traceback (most recent call last):
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: myapp_entry
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 219, in wrapper
response = callback(request, *args, **kwargs)
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 450, in dispatch_list
return self.dispatch('list', request, **kwargs)
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 482, in dispatch
response = method(request, **kwargs)
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 1335, in get_list
to_be_serialized = paginator.page()
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/paginator.py", line 194, in page
count = self.get_count()
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/paginator.py", line 126, in get_count
return self.objects.count()
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 369, in count
return self.query.get_count(using=self.db)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 476, in get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 457, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: myapp_entry
"}
有人能看出我做错了什么吗?或者教程不适用于这些版本?
sqlite3.OperationalError: no such table: myapp_entry
首先,您的项目使用的是 sqlite,而不是 mysql。您应该更新 DATABASES
设置。
将 myapp
添加到项目设置中的 INSTALLED_APPS
。
然后,运行 ./manage.py migrate
。修复 INSTALLED_APPS
后,Django 应该能够找到应用程序的 Entry
模型并为其创建 table。
正如页面标题所说,我 Getting Started with Tastypie 按照链接的教程进行操作。但是当我尝试加载 /api/entry/?format=json
时,我收到 HTTP 500 响应,并显示以下错误消息:
没有这样的 table: myapp_entry
当我查看sqlite3时,确实没有table。
以下是我学习教程的方法:
$ django-admin startproject mysite $ cd mysite $ django-admin startapp myapp
我created/edited myapp/models.py
, myapp/api.py
和 mysite/urls.py
按照教程中的说明,并在[中将 'tastypie' 添加到我的 INSTALLED_APPS
=19=].
注意:我不清楚要编辑或创建哪个 urls.py
文件,所以我编辑了mysite
中已有一个。所以现在它看起来像这样:
from django.conf.urls import url, include
from myapp.api import EntryResource
from django.contrib import admin
entry_resource = EntryResource()
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^blog/', include('myapp.urls')),
url(r'^api/', include(entry_resource.urls)),
]
我注释掉了 'blog' 行,因为它导致了错误 ImportError: No module named 'myapp.urls'。我认为这是我正在填充的步骤,但是当我尝试将教程代码放入 myapp/urls.py
时,我在尝试加载页面时收到了 404,然后我尝试添加 url(r'^blog/', include('myapp.urls'))
到 mysite/urls.py
,我得到了堆栈溢出。所以我回到上面显示的代码。
明确地说,这是我的文件结构现在的样子:
$ find . -type f -not -name '*.pyc' ./manage.py ./myapp/__init__.py ./myapp/views.py ./myapp/models.py ./myapp/tests.py ./myapp/admin.py ./myapp/apps.py ./myapp/migrations/__init__.py ./myapp/api.py ./db.sqlite3 ./mysite/__init__.py ./mysite/settings.py ./mysite/urls.py ./mysite/wsgi.py
我做的另一个改变是在我的 Entry
class 中添加一个 Meta
subclass,所以前十几行看起来像这样:
class Entry(models.Model):
user = models.ForeignKey(User)
pub_date = models.DateTimeField(default=now)
title = models.CharField(max_length=200)
slug = models.SlugField(null=True, blank=True)
body = models.TextField()
class Meta:
app_label = 'myapp'
# __unicode__() and save() as in the tutorial
如果我不这样做,我会在控制台中得到这个:RuntimeError: Model class myapp.models.Entry doesn't declare explicit app_label and不在 INSTALLED_APPS.
的应用程序中我尝试的另一件事是 python manage.py migrate
。它找到了要做的事情,但这并没有解决这个错误。
这是我安装的(在 requirements.txt
/virtualenv
中):
- Django (1.10)
- django-tastypie (0.13.3)
- PyYAML (3.12)
- mysqlclient (1.3.10) (虽然我还没有真正设置它——我现在使用默认的sqlite3设置)
我是 运行 Python 3.4.3,但是我在 Python 2.7.6.
中使用非常相似的设置得到了完全相同的错误最后,这是来自该 HTTP 500 页面的完整堆栈跟踪:
{"error_message": "no such table: myapp_entry", "traceback": "Traceback (most recent call last): File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table: myapp_entry The above exception was the direct cause of the following exception: Traceback (most recent call last): File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 219, in wrapper response = callback(request, *args, **kwargs) File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 450, in dispatch_list return self.dispatch('list', request, **kwargs) File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 482, in dispatch response = method(request, **kwargs) File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 1335, in get_list to_be_serialized = paginator.page() File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/paginator.py", line 194, in page count = self.get_count() File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/paginator.py", line 126, in get_count return self.objects.count() File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 369, in count return self.query.get_count(using=self.db) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 476, in get_count number = obj.get_aggregation(using, ['__count'])['__count'] File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 457, in get_aggregation result = compiler.execute_sql(SINGLE) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql cursor.execute(sql, params) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise raise value.with_traceback(tb) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table: myapp_entry "}
有人能看出我做错了什么吗?或者教程不适用于这些版本?
sqlite3.OperationalError: no such table: myapp_entry
首先,您的项目使用的是 sqlite,而不是 mysql。您应该更新 DATABASES
设置。
将 myapp
添加到项目设置中的 INSTALLED_APPS
。
然后,运行 ./manage.py migrate
。修复 INSTALLED_APPS
后,Django 应该能够找到应用程序的 Entry
模型并为其创建 table。