我想通過下面的請求標題發佈device_id是我的代碼,但我無法發佈它。我通過postman中的標題發送device_id。在django rest框架api中通過請求標題發佈device_id
PLZ避免的間距
views.py
class DeviceID(APIView):
def post(self, request):
device_id = request.META.get('HTTP_X_DEVICE', '')
serializer = DeviceSerializer(data=device_id)
if serializer.is_valid():
serializer.save()
else:
return Response(serializer.errors)
serializers.py
class DeviceSerializer(serializers.ModelSerializer):
DEVICE = serializers.IntegerField()
class Meta:
model = Device
fields = 'DEVICE'
def create(self, validated_data):
return Device.objects.create(**validated_data)
models.py
class Device(models.Model):
DEVICE = models.IntegerField()
urls.py
url(r'^device/', views.DeviceID.as_view()),
感謝這是準確的 –
你能告訴我如何將這個設備id包含在我的request.data中並將它傳入串行器。這是代碼device_id = request.META.get('HTTP_DEVICE','') serializer = UserSerializer(data = request.data) –
感謝您的幫助 –