Django Rest Framework网址过滤器,导入视图错误

时间:2018-11-16 10:49:34

标签: python django django-rest-framework django-views

我目前无法编写通过URL过滤数据的Django Rest Framework。由于某种原因,问题似乎出在我导入视图时。错误指向在我在urls.py

中添加url过滤器之前可以正常工作的模型
File "/home/jd_dhang/drf-demo/project/urls.py", line 5, in <module>
    from apps.core.views import SurgeryView
  File "/home/jd_dhang/drf-demo/project/apps/core/views.py", line 2, in <module>
    from .models import University, Student, SurgeryType
  File "/home/jd_dhang/drf-demo/project/apps/core/models.py", line 3, in <module>
    class University(models.Model):
  File "/home/jd_dhang/drf-demo/venv/lib/python3.5/site-packages/django/db/models/base.py", line 102, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class apps.core.models.University doesn't declare an explicit app_label and isn't in an applica
tion in INSTALLED_APPS.

Django == 1.9.13,djangorestframework == 3.6.0

project / urls.py

**from apps.core.views import SurgeryView**

urlpatterns = [
    **url(r'^api/surgery/(?P<surgery_type>.+)/$', SurgeryView.as_view())**,
    ...
]

project / apps / core / views.py

from rest_framework import viewsets
from .models import University, Student, SurgeryType
from .serializers import UniversitySerializer, StudentSerializer, SurgeryTypeSerializer

class StudentViewSet(viewsets.ModelViewSet):
...
class UniversityViewSet(viewsets.ModelViewSet):
...

class SurgeryView(viewsets.ModelViewSet): #(APIView):
    queryset = SurgeryType.objects.all()
    serializer_class = SurgeryTypeSerializer
    def get_queryset(self):
        queryset = SurgeryType.objects.all()
        surgery_type = self.kwargs['surgery_type']
        return SurgeryType.objects.filter(surgery_type=surgery_type)

如果有帮助,这是我的模特

project / apps / core / models.py

from django.db import models

class University(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        verbose_name = "University"
        verbose_name_plural = "Universities"

    def __str__(self):
        return self.name

class Student(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    university = models.ForeignKey(University)

    class Meta:
        verbose_name = "Student"
        verbose_name_plural = "Students"

    def __str__(self):
        return '%s %s' % (self.first_name, self.last_name)

class SurgeryType(models.Model):
    surgery_type = models.CharField("surgery_type", max_length=200)
    sub_type = models.IntegerField(default=1)
    available = models.BooleanField()
    url = models.URLField(max_length=200, default='https://www.google.com')

    def __str__(self):
        return 'Type: ' + self.surgery_type + ' SubType : ' + str(self.sub_type)

谢谢

project / conf / base.py

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
    'rest_framework',
    'rest_framework_swagger'
]

1 个答案:

答案 0 :(得分:0)

在您的urls.py中修复导入。

应为from core.views import SurgeryView而不是from apps.core.views import SurgeryView