2016-06-13 4 views
0

안녕 얘들 아, DRF 끝점을 호출 할 때 제 3 자 API에서 데이터를 가져 오려고합니다.장고 Rest 프레임 워크에서 json 데이터를 가져 오는 방법은 무엇입니까?

나는 urllib2를 사용하여 get 요청을 쉽게 빌드하고 json을 비 직렬화 할 수 있지만 장고 직렬기를 사용하고 drf 방법을 사용하는 방법이 있습니까? 아니면 이걸 지나치는거야?

import json, urllib2 


class IngestThirdPartyView(APIView): 
    def post(self, request): 
     data = request.data 
     url = 'https://third_party.com/media/{}'.format(data['item_id]') 
     response = urllib.urlopen(url) 
     # Handle item somehow 
     item = json.load(response) 

더 많은 DRF 친화적 인 방법으로 해결할 수있는 제안 사항은 매우 환영합니다!

건배

귀하의 예제 코드에서

답변

0

,

import requests 

class IngestThirdPartyView(): 
    def post(self, request): 
     data = request.data 
     url = 'https://third_party.com/media/{}'.format(data['item_id]') 
     item = requests.get(url).json() # this will retrieve json response 
     # then you could deserialize the above json and validate it or something 
     # as described here in docs http://www.django-rest-framework.org/api-guide/serializers/#deserializing-objects 
     # Handle item somehow 
관련 문제