Django:模型类没有在INSTALLED_APPS的应用程序中声明一个显式的app_label并且不是

时间:2017-06-05 16:14:18

标签: python django django-rest-framework

我正在努力将Django Rest Framework实现到我当前的Django项目中。我已经按照DRF教程/入门指南进行了一些小的改动,以允许drf代码与我现有的应用程序和模型一起使用。

我的问题是每当我尝试运行应用程序时都会收到以下错误:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f83fb31dd08>
Traceback (most recent call last):
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/vagrant/PROJECT/config/urls.py", line 26, in <module>
url(r'^api/', include('api.urls')),
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/vagrant/PROJECT/api/urls.py", line 2, in <module>
from . import views
  File "/vagrant/PROJECT/api/views.py", line 5, in <module>
from notebook.notes.models import Note
  File "/vagrant/PROJECT/notes/models.py", line 13, in <module>
class Notebook(models.Model):
  File "/home/vagrant/.virtualenvs/python/lib/python3.5/site-packages/django/db/models/base.py", line 118, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class PROJECT.notes.models.Notebook doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

说明:

我有一个名为notes的现有应用,随着DRF的推出,我创建了一个名为api的新应用,它将处理所有&#34; api逻辑&#34;。在api应用中,我从notes应用导入模型,并在需要时使用它们(序列化器和视图)。

我的代码

Notes app

models.py

class Notebook(models.Model):
    title = models.CharField(max_length=250)

class Note(models.Model):
    content = models.TextField(blank=True, null=True)
    notebook = models.ForeignKey(Notebook, on_delete=models.CASCADE, related_name='notes')

Api app

urls.py

from django.conf.urls import url
from . import views

app_name = 'api'

urlpatterns = [
    url(r'notes/$', views.note_list),
    url(r'notes/(?P<pk>[0-9]+)/$', views.note_detail),
]

views.py

from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from PROJECT.notes.models import Note
from .serializers import NoteSerializer


@csrf_exempt
def note_list(request):

    if request.method == "GET":
        notes = Note.objects.all()
        serializer = NoteSerializer(notes, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == "POST":
        data = JSONParser().parse(request)
        serializer = NoteSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


@csrf_exempt
def note_detail(request, pk):
    try:
        note = Note.objects.get(pk=pk)
    except Note.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == "GET":
        serializer = NoteSerializer(note)
        return JsonResponse(serializer.data)

    elif request.method == "PUT":
        data = JSONParser().parse(request)
        serializer = NoteSerializer(note, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        note.delete()
        return HttpResponse(status=204)

Root config

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^notes/', include('notes.urls')),
    url(r'^api/', include('api.urls')),
]

settings.py

DJANGO_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

THIRD_PARTY_APPS = (
    'rest_framework',
)

LOCAL_APPS = (
    'notes.apps.NotesConfig',
    'api.apps.ApiConfig',
)

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

我尝试了什么

我有点迷失在这里,所以我没有尝试过很多。但我的一个理论是notes应用中的模型可能没有正确加载,或者应用程序没有按照我在settings.py中列出的顺序加载。< / p>

我猜这个问题是一个新手的错误,但我无法找到我做错的事。

我试图通过删除不相关的东西来清理代码。如果缺少某些东西,只需询问,我会将其包括在内。

0 个答案:

没有答案