2

私は残りのAPIにDjangoRestFrameworkを使用しています

get リクエスト で テーブルにデータがない場合、レスポンスは空であると表示されます。テーブルにデータがある場合、応答には空のサーバー エラー (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')

ビュー.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
4

1 に答える 1

3

DRF バージョン 3+ は Django 1.1 (上記のスタックトレースにあります) では動作しません。Django 1.7+ が必要です。Django の最新バージョンにアップグレードし、ビューのメソッドformat=Noneから削除します。.get()

于 2015-12-05T08:31:06.737 に答える