通过将json数据移动到url来减少流量

时间:2017-03-13 05:24:48

标签: python django django-rest-framework

我很想在django rest框架中开发API。我想为app mobile创建API。

为减少流量,我需要删除" user_id"来自json,并从url中确定它。我该怎么办?

URL:

class UserMeasurement(models.Model):
    user_id = models.IntegerField(blank=True, null=True)
    numb = models.DecimalField(max_digits=20, decimal_places=10, blank=True, null=True)
    measurent_code = models.CharField(max_length=50, null=True)


    def __unicode__(self):
        return self.sync_id

    def save(self, *args, **kwargs):
        return super(UserMeasurement, self).save(*args, **kwargs) 

    class Meta:
        managed = False
        db_table = 'user_measurement'

模型

class UserMeasurementSerializer(serializers.HyperlinkedModelSerializer):

   class Meta:
        model = UserMeasurement
        fields = ('user_id', 'numb', 'measurent_code',)

串行器:

def usermeasurement (request,user):

    try:
        usermeasurement = UserMeasurement.objects.filter(user_id=user)
    except UserMeasurement.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    elif request.method == 'POST':
        uv = UserMeasurement(user_id=user)
        serializer = UserMeasurementSerializer(uv, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

的观点:

document.getElementById("testItem").onclick = function () {
  this.classList.toggle('transition');
}

1 个答案:

答案 0 :(得分:0)

你的意思是在想要从url获取user_id的视图中吗?

如下:

的观点:

def usermeasurement (request, *args, **kwargs):
    user_id = kwargs['user']
    ....