Python Django:Model类不';不要声明明确的应用程序标签和isn';t在已安装的应用程序中的应用程序中

Python Django:Model类不';不要声明明确的应用程序标签和isn';t在已安装的应用程序中的应用程序中,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在将Django Rest框架实现到我当前的Django项目中。我遵循了DRF教程/入门指南,并做了一些小改动,以允许DRF代码与我现有的应用程序和模型配合使用 我的问题是,每当我尝试运行应用程序时,都会出现以下错误: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f83fb31dd08> Traceback (most recent cal

我正在将Django Rest框架实现到我当前的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.
Api应用程序 url.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')
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),
]
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)
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^notes/', include('notes.urls')),
    url(r'^api/', include('api.urls')),
]
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
根配置 url.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')
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),
]
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)
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^notes/', include('notes.urls')),
    url(r'^api/', include('api.urls')),
]
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
设置.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')
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),
]
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)
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^notes/', include('notes.urls')),
    url(r'^api/', include('api.urls')),
]
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
中列出的顺序加载

我猜问题是新手犯的错误,但我找不到我做错了什么


我试图通过删除不相关的东西来清理代码。如果缺少什么,只要问一下,我就会把它包括进去。

请出示
说明配置
。因为已从notebook.notes.models导入Note进行了
回溯。我希望您在
应用程序中安装
'notebook.notes'
,但您有
notes.APPS.NotesConfig
。用
PROJECT
替换实际值可能会隐藏问题。您可以尝试将导入从notes.models import Note'
更改为
。如果应用程序可以作为
notes
notebook.notes
导入,则表明您的Python路径有一些不寻常的地方。