Created
February 1, 2017 06:47
-
-
Save ragowthaman/968ad718af6b1fd38a4770aad2867319 to your computer and use it in GitHub Desktop.
Create a webservice for BULK create
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import * | |
urlpatterns = patterns('', | |
# list | |
url(r'^webservice/record/gyro/$', 'spothole.views.record_gyro_via_webservice'), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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