“请求”对象不可迭代

时间:2018-01-18 06:00:05

标签: python django django-rest-framework

我正在尝试使用django rest框架进行简单的crud演示项目API for mobile并获得以下错误

enter image description here

项目名称: crud_demo

应用名称:crud_operations

crud_demo / urls.py

from django.conf.urls import include, url
from django.urls import path  

urlpatterns= [
    path('models/', views.model_list),
    path('models/<int:pk>/', views.model_detail),
]

crud_operations / models.py

  from django.db import models

# Create your models here.

class Member(models.Model):
    firstname = models.CharField(max_length=40)
    lastname = models.CharField(max_length=40)

crud_operations / serializers.py

from crud_operations.models import Member
from rest_framework import serializers

class MemberSerializer(serializers.ModelSerializer):
    class Meta:
        model = Member
        fields = ('firstname', 'lastname')

crud_operations / views.py

from django.shortcuts import render, redirect
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from .models import Member
from crud_operations.serializers import MemberSerializer

@api_view(['GET', 'POST'])
def model_list(request):
    if request == 'GET':
        member = Member.objects.all()
        serializer = MemberSerializer(member)
        return Response(serializer.data)
    elif request == 'POST':
        serializer = MemberSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    return Response({'key': request}, status=status.HTTP_200_OK)


@api_view(['GET', 'PUT', 'DELETE'])
def model_detail(request, pk):
    try:
        member = Member.objects.get(pk=pk)
    except Member.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request == 'GET':
        serializer = MemberSerializer(member)
        return Response(serializer.data)

    elif request == 'PUT':
        serializer = MemberSerializer(member, data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        member.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

    return Response({'key': 'value'}, status=status.HTTP_200_OK)

1 个答案:

答案 0 :(得分:4)

@api_view(['GET', 'POST'])
def model_list(request):
    if request.method == 'GET':
        member = Member.objects.all()
        serializer = MemberSerializer(member)
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = MemberSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    return Response({'key': request}, status=status.HTTP_200_OK)

请求内容包含大量对象,因此要检查类型,需要使用method对象的HttpRequest属性来检查请求的类型,请尝试以上操作ANS