Skip to content

Instantly share code, notes, and snippets.

@ragowthaman
Created February 1, 2017 06:47
Show Gist options
  • Save ragowthaman/968ad718af6b1fd38a4770aad2867319 to your computer and use it in GitHub Desktop.
Save ragowthaman/968ad718af6b1fd38a4770aad2867319 to your computer and use it in GitHub Desktop.
Create a webservice for BULK create
class Gyro(models.Model):
device = models.ForeignKey(Device)
ts = models.DateTimeField()
x = models.DecimalField(max_digits=18, decimal_places=18)
y = models.DecimalField(max_digits=18, decimal_places=18)
z = models.DecimalField(max_digits=18, decimal_places=18)
from models import *
from rest_framework import serializers
from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin, ListBulkCreateUpdateDestroyAPIView
class GyroSerializer(serializers.ModelSerializer):
class Meta:
model = Gyro
fields = ('device', 'ts', 'x', 'y', 'z')
from django.conf.urls import *
urlpatterns = patterns('',
# list
url(r'^webservice/record/gyro/$', 'spothole.views.record_gyro_via_webservice'),
)
@api_view(['GET', 'PUT'])
def record_gyro_via_webservice(request):
"""
Record the gyroscope data coming from mobile application spothole
:param request:
:return: success
"""
# do the operations
if request.method == "PUT":
print (request.data)
serializer = GyroSerializer(data=request.data, many=True)
print serializer
if serializer.is_valid():
print "serializer is valid"
serializer.save()
return Response(status=status.HTTP_201_CREATED)
else:
print "serializer is NOT valid"
print serializer.error_messages
print serializer.error
print serializer.data
return Response(serializer.errors, status=status.HTTP_204_NO_CONTENT)
elif request.method == 'GET':
serializer = GyroSerializer(Gyro.objects.all()[:4], many=True)
return Response(serializer.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment