Django Rest 框架 - 如果 table 包含显示服务器错误 (500) 的响应数据

Django Rest framework - If table contains data in response showing Server Error (500)

我正在使用 DjangoRestFramework 休息 api

在get请求中 如果 table 中没有数据,则响应显示为空。 如果 table 中有数据,则响应显示空服务器错误 (500)。

下面是我的代码

models.py

from __future__ import unicode_literals
from django.db import models

# Create your models here.
class Employee(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=200)

serializers.py

from rest_framework import serializers
from firstexample.models import Employee

class employeeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Employee
        fields = ('name', 'address')

views.py

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets
from firstexample.models import Employee
from firstexample.serializers import employeeSerializer
# Create your views here.


class employeeList(APIView):

    def get(self, request, format=None):
        employees = Employee.objects.all()
        serializer = employeeSerializer(employees, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = employeeSerializer(data=request.data)
        print(request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

urls.py

from django.conf.urls import url
from django.contrib import admin
from firstexample import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^employeesList/$', views.employeeList.as_view()),
]

谁能帮帮我。我花了更多时间在这上面,但我没有弄清楚问题所在。

提前致谢。

错误代码

Performing system checks...

System check identified no issues (0 silenced).
December 05, 2015 - 07:50:03
Django version 1.10.dev20151112003354, using settings 'helloworld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
<QuerySet [<Employee: Employee object>]>
Internal Server Error: /employeesList/
Traceback (most recent call last):
  File "/Users/jaikakkar/Desktop/Python/django/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/jaikakkar/Desktop/Python/django/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jaikakkar/Desktop/Python/django/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/jaikakkar/Desktop/Python/django/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/jaikakkar/Desktop/pythonlearning/helloworld/firstexample/views.py", line 17, in get
    print(serializer.data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py", line 674, in data
    ret = super(ListSerializer, self).data
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py", line 239, in data
    self._data = self.to_representation(self.instance)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py", line 614, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py", line 459, in to_representation
    fields = self._readable_fields
  File "/Users/jaikakkar/Desktop/Python/django/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py", line 353, in _readable_fields
    field for field in self.fields.values()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py", line 339, in fields
    for key, value in self.get_fields().items():
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py", line 918, in get_fields
    info = model_meta.get_field_info(model)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/utils/model_meta.py", line 69, in get_field_info
    reverse_relations = _get_reverse_relationships(opts)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/utils/model_meta.py", line 137, in _get_reverse_relationships
    for relation in opts.get_all_related_objects():
AttributeError: 'Options' object has no attribute 'get_all_related_objects'
[05/Dec/2015 07:50:04] "GET /employeesList/ HTTP/1.1" 500 114115

DRF 版本 3+ 不适用于 Django 1.1(在上面的堆栈跟踪中),它需要 Django 1.7+。升级到最新版本的 Django 并从您认为的 .get() 方法中删除 format=None