serializers.py:
from django.forms import widgets
from rest_framework import serializers
from .models import Classname, Sectionname, Teachername, Attendancename
from django.contrib.auth.models import User
class ClassSerializer(serializers.HyperlinkedModelSerializer): #Once I add the Hyperlinked it raises above exception.
class Meta:
model = Classname
fields = ('url', 'class_name',)
class SectionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Sectionname
fields = ('url', 'class_name', 'section_name')
class TeacherSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Teachername
fields = ('url', 'classname', 'secname', 'teachname')
class AttendanceSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Attendancename
fields = ('url', 'teacher_name', 'date', 'intime', 'outtime')
urls.py:
from django.conf.urls import url, patterns
from . import views
urlpatterns = [
#DRF urls start from here...
url(r'^$', views.api_root),
#url(r'^users/$', views.UserList.as_view(), name='user-list'),
#url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view(), name='user-detail'),
url(r'^class/$', views.ClassList.as_view(), name='classname-list'),
url(r'^class/(?P<pk>[0-9]+)/$', views.ClassDetail.as_view(), name='classname-detail'),
url(r'^section/$', views.SectionList.as_view(), name='sectionname-list'),
url(r'^section/(?P<pk>[0-9]+)/$', views.SectionDetail.as_view(), name='sectionname-detail'),
url(r'^teach/$', views.TeacherList.as_view(), name='teachername-list'),
url(r'^teach/(?P<pk>[0-9]+)/$', views.TeacherDetail.as_view(), name='teachername-detail'),
url(r'^attend/$', views.AttendanceList.as_view(), name='attendance-list'),
url(r'^attend/(?P<pk>[0-9]+)/$', views.AttendanceDetail.as_view(), name='attendance-detail'),
views.py:
from django.contrib.auth.models import User
from .models import Classname, Sectionname, Teachername, Attendancename
from rest_framework import generics
from rest_framework.reverse import reverse
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .serializers import ClassSerializer, SectionSerializer, TeacherSerializer, AttendanceSerializer
from rest_framework.permissions import IsAuthenticatedOrReadOnly
@api_view(('GET',))
def api_root(request, format=None):
return Response({
'class':reverse('student:classname-list', request=request, format=format),
'section':reverse('student:sectionname-list', request=request, format=format),
'teacher':reverse('student:teachername-list', request=request, format=format),
'attendance':reverse('student:attendancename-list', request=request, format=format)
})
class ClassnameList(generics.ListCreateAPIView):
queryset = Classname.objects.all()
serializer_class = ClassSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
class ClassnameDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Classname.objects.all()
serializer_class = ClassSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
class SectionnameList(generics.ListCreateAPIView):
queryset = Sectionname.objects.all()
serializer_class = SectionSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
class SectionnameDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Sectionname.objects.all()
serializer_class = SectionSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
class TeachernameList(generics.ListCreateAPIView):
queryset = Teachername.objects.all()
serializer_class = TeacherSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
class TeachernameDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Teachername.objects.all()
serializer_class = TeacherSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
class AttendancenameList(generics.ListCreateAPIView):
queryset = Attendancename.objects.all()
serializer_class = AttendanceSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
class AttendancenameDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Attendancename.objects.all()
serializer_class = AttendanceSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
我的追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/stu/class/
Django Version: 1.8.3
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'student')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "C:\Users\User\demo\venv\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\views.py" in dispatch
456. response = self.handle_exception(exc)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\views.py" in dispatch
453. response = handler(request, *args, **kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\generics.py" in get
241. return self.list(request, *args, **kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\mixins.py" in list
47. return Response(serializer.data)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\serializers.py" in data
622. ret = super(ListSerializer, self).data
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\serializers.py" in data
217. self._data = self.to_representation(self.instance)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\serializers.py" in to_representation
572. self.child.to_representation(item) for item in iterable
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\serializers.py" in <listcomp>
572. self.child.to_representation(item) for item in iterable
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\serializers.py" in to_representation
439. ret[field.field_name] = field.to_representation(attribute)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\relations.py" in to_representation
272. raise ImproperlyConfigured(msg % self.view_name)
Exception Type: ImproperlyConfigured at /stu/class/
Exception Value: Could not resolve URL for hyperlinked relationship using view name "classname-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
我正在关注DRF教程,以使我的演示应用程序可序列化。当我尝试在序列化程序中添加超链接时,我遇到了一个问题,它会抛出异常。
我不知道为什么会这样做?因为我遵循正确的URL命名空间约定。
请帮我解决一下.....
答案 0 :(得分:1)
实际上,问题在于我的项目级网址。因为我从我的项目级网址中删除了'namespacing'
参数,其中包括我的应用程序
'url(r'^stu/$', include('student:app'))'
。由于'classname-detail'
不能包含隐式'namespacing'
参数,因此它会超出异常。