gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:07 31 4
gpt4 key购买 nike

我正在使用 DjangoRestFramework 作为 rest api

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

下面是我的代码

模型.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)

序列化器.py

from rest_framework import serializers
from firstexample.models import Employee

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

View .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)

网址.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 并从 View 中的 .get() 方法中删除 format=None

关于python - Django Rest 框架 - 如果表包含显示服务器错误 (500) 的响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102111/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com