Search code examples
djangorestdjango-modelstastypie

Getting Started with Tastypie: no such table: myapp_entry


Just like the title of the page says, I'm Getting Started with Tastypie by following the linked tutorial. But when I try to load /api/entry/?format=json, I get HTTP 500 response, with this error message:

no such table: myapp_entry

When I look in sqlite3, indeed, there's no such table.

Here's how I followed the tutorial:

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

I created/edited myapp/models.py, myapp/api.py and mysite/urls.py as specified in the tutorial, and added 'tastypie' to my INSTALLED_APPS in mysite/settings.py.

Note: It wasn't clear to me which urls.py file to edit or create, so I edited the existing one in mysite. So now it looks like this:

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)),
]

I commented out the 'blog' line, because it caused the error ImportError: No module named 'myapp.urls'. I think this is the step I'm stuffing up, but when I tried putting the tutorial code in myapp/urls.py instead, I got a 404 when I tried loading the page, and when I then tried adding url(r'^blog/', include('myapp.urls')) to mysite/urls.py, I got a stack overflow. So I've gone back to the code as shown above.

To be clear, here's what my file structure looks like now:

$ 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

The one other change I made was adding a Meta subclass to my Entry class, so the first dozen lines look like this:

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

If I don't do that, I get this in the console: RuntimeError: Model class myapp.models.Entry doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Another thing I tried was python manage.py migrate. It found things to do, but this hasn't fixed this error.

Here's what I've got installed (in requirements.txt/virtualenv):

  • Django (1.10)
  • django-tastypie (0.13.3)
  • PyYAML (3.12)
  • mysqlclient (1.3.10) (although I haven't actually set it up yet—I'm using the default sqlite3 setup for now)

I'm running Python 3.4.3, but I got exactly the same error using a quite similar setup in Python 2.7.6.

Finally, here's the full stack trace from that HTTP 500 page:

{"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
"}

Can anybody see what I'm doing wrong? Or does the tutorial not apply to these versions?


Solution

  • sqlite3.OperationalError: no such table: myapp_entry

    For one, your project is using sqlite, not mysql. You should update your DATABASES setting.

    Add myapp to your INSTALLED_APPS in your project settings.

    Then, run ./manage.py migrate. After fixing INSTALLED_APPS Django should be able to find the app's Entry model and create the table for it.