Python 请求后Django REST框架

Python 请求后Django REST框架,python,django,rest,post,django-rest-framework,Python,Django,Rest,Post,Django Rest Framework,这是我的观点 from rest_framework import generics, permissions from .models import Survey,Response from .serialize import SurveySerializer,ResponseSerializer,SurveySerializerQuestion from rest_framework.decorators import api_view class SurveyList(generics.

这是我的观点

from rest_framework import generics, permissions
from .models import Survey,Response
from .serialize import SurveySerializer,ResponseSerializer,SurveySerializerQuestion
from rest_framework.decorators import api_view

class SurveyList(generics.ListAPIView):
    model = Survey
    serializer_class = SurveySerializerQuestion

    def get_queryset(self):

        return Survey.objects.all()


class SurveyListByID(generics.ListAPIView):
    model = Survey
    serializer_class = SurveySerializer

    def get_queryset(self):
        survey_id = self.kwargs['survey_id']
        return Survey.objects.filter(survey_id = survey_id)


class ResponseList(generics.ListAPIView):

    model = Response
    serializer_class = ResponseSerializer

    def get_queryset(self):
        survey_id = self.kwargs['survey_id']
        return Response.objects.filter(survey_id = survey_id)
这是我的URL.py

from django.conf.urls import patterns, include, url
from views import SurveyList,SurveyListByID,ResponseList
urlpatterns = patterns('',
url(r'^surveys/$', SurveyList.as_view(),name ='survey-list') ,
url(r'^surveys/(?P<survey_id>[0-9]+)/$', SurveyListByID.as_view(),name ='survey-list-by-id'), 
url(r'^response/(?P<survey_id>[0-9]+)/$', ResponseList.as_view(),name ='response-list'), 
)
最后是models.py

import re
from django.db import models
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError

def alphanumeric_test(val):
    if not re.match('^[0-9a-zA-Z]*$', val):
        raise ValidationError('Only alphanumeric characters are allowed.')

def alphabetic_test(val):
    if not re.match('^[a-zA-Z]*$', val):
        raise ValidationError('Please enter alphabetic value.')

class Survey(models.Model):
    survey_id = models.AutoField(primary_key=True)
    question = models.CharField(max_length = 300)
    choice1 = models.CharField(max_length = 100)
    choice2 = models.CharField(max_length = 100)
    def __unicode__(self):              
        return u'%s %s %s' % (self.question,self.choice1,self.choice2)


class Response(models.Model):
        survey_id = models.ForeignKey(Survey, blank=True, null=True, on_delete=models.SET_NULL)
        choice1_count = models.IntegerField()
        choice2_count = models.IntegerField()
        def __unicode__(self):              
    return u'%s %s %s' % (self.survey_id,self.choice1_count,self.choice2_count)
现在,我如何在没有UI和使用Django Rest浏览器的情况下在Django Rest中编写POST请求。
我想做一个POST请求,使用与get类似的url捕获调查中的选择。可能吗?

在views.py中添加一个新类,如下所示:

class SurveyAPIView(APIView):

    def post(self, request, format=None):
        serializer = SurveySerializer(request.data)
        if serializer.is_valid():
            instance = serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
    else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
并在URL.py中添加一个新行,如:

url(r'^create-survey/$', SurveyAPIView.as_view(),name ='create-survey') ,

谢谢但是如何在url中记录选择呢?你快乐吗?是或否。我只想捕获是或否,并增加与调查结果相匹配的响应表列中的计数。我的想法是,“choice1”可能是“是”,而“choice2”可能是“否”,然后响应表中的计数将是“是”响应的计数(存储在Response.choice1_count中)和“否”响应的计数(存储在choice2_count中)?在上面的示例中验证序列化程序后,您可以将响应值设置为serializer.data['choice1']和serializer.data['choice2']。选择可以是任何内容。例如,您喜欢哪种饮料?茶咖啡OK。通过序列化程序访问选择响应也是如此。data['choice1'](等等)您希望在响应表中记录选择的内容?
url(r'^create-survey/$', SurveyAPIView.as_view(),name ='create-survey') ,