Tastypie入门:没有这样的表:myapp_entry

时间:2017-02-27 20:29:21

标签: django rest django-models tastypie

就像页面标题所说的那样,我是Getting Started with Tastypie之后的链接教程。但是当我尝试加载/api/entry/?format=json时,我收到HTTP 500响应,并显示以下错误消息:

没有这样的表:myapp_entry

当我查看sqlite3时,确实没有这样的表。

以下是我遵循教程的方法:

$ django-admin startproject mysite
$ cd mysite
$ django-admin startapp myapp

我按照教程中的说明创建/编辑了myapp/models.pymyapp/api.pymysite/urls.py,并将{tastypie'添加到INSTALLED_APPS中的mysite/settings.py。< / p>

注意:我不清楚要编辑或创建哪个 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:没有名为'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

我做的另一个更改是在我的Meta类中添加了Entry子类,所以前十几行看起来像这样:

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类myapp.models.Entry没有声明一个显式的app_label,也不在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
"}

有人能看出我做错了什么吗?或者本教程不适用于这些版本?

1 个答案:

答案 0 :(得分:2)

sqlite3.OperationalError: no such table: myapp_entry

首先,你的项目是使用sqlite,而不是mysql。您应该更新DATABASES设置。

在项目设置中将myapp添加到INSTALLED_APPS

然后,运行./manage.py migrate。修复INSTALLED_APPS后,Django应该能够找到应用程序的Entry模型并为其创建表格。