2
這是我的views.pyPOST請求Django的REST框架
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)
這是我的urls.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'),
)
這是serialize.py
from rest_framework import serializers
from .models import Employee,Survey,EmployeeSurvey,Response
class SurveySerializer(serializers.ModelSerializer):
class Meta:
model = Survey
fields = ('survey_id', 'question', 'choice1','choice2')
class SurveySerializerQuestion(serializers.ModelSerializer):
class Meta:
model = Survey
fields = ('survey_id', 'question')
class ResponseSerializer(serializers.ModelSerializer):
class Meta:
model = Response
fields = ('id','survey_id','choice1_count','choice2_count')
終於這是模型.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 Browser的情況下在Django Rest中編寫POST請求。 我想做一個POST請求來捕獲調查中的選擇,使用像我這樣的網址來獲得。這可能嗎?
謝謝!但如何記錄在URL中的選擇?你快樂嗎?是或否我只想捕捉是或否,並在響應表列中增加與調查ID相匹配的計數 –
「choice1」可能爲「是」且「choice2」可能爲「否」,然後計數爲響應表將是Yes響應的計數(存儲在Response.selection1_count中)和沒有響應的計數(存儲在choice2_count中)?在以上示例中驗證了序列化器之後,您可以獲取響應值,如serializer.data ['choice1']和serializer.data ['choice2']。 – nmgeek
選擇可以是任何東西,比如你喜歡哪種飲料?茶咖啡 –