2017-10-13 5 views
0

Cloudny의 업로드 위젯을 설정하고 결과 Cloudnight objet을 내 모델의 CloudinaryField()에 저장하려고합니다. Cloudinary의 sample project{{form}}을 사용하여 이미지를 업로드하는 방법 만 보여줍니다.Django - 업로드 위젯을 통해 Cloudny Object 저장

위젯을 사용하여 업로드 할 때 사전을 다시 얻지 만 문서 저장 방법이있는 곳을 찾을 수 없습니다.

+0

"사전"이란 생성 된 숨겨진 입력 필드의 값인 식별자 문자열 또는 POST 요청에 의해 반환 된 JSON 응답을 의미합니까? –

+0

POST 요청에 의해 Cloudinary에 반환되는 값 집합입니다. 나는 그것이 JSON이라고 생각하지만 백엔드로 곧바로 보내고 파이썬은 이것을 사전으로 인식하는 것 같다. 어쩌면 나는 한 걸음 뛸거야? –

답변

1

업로드 위젯에는 알림 URL을 포함 할 수있는 옵션이 있으며 업로드 사전 설정 설정에서 구성 할 수 있습니다. 이 알림 URL은 이미지 모델을 저장하는 데 사용할 수있는 응답을 서버 끝점으로 반환합니다.

업로드 응답에서 필요한 CloudinaryField를 생성하려면 CloudinaryResource 객체를 사용할 수 있습니다.

from cloudinary import CloudinaryResource 

. . . 

json_response = { "public_id": "test", "type": "upload", . . . } 

# Populate a CloudinaryResource object using the upload response 
result = CloudinaryResource(public_id=json_response['public_id'], type=json_response['type'], resource_type=json_response['resource_type'], version=json_response['version'], format=json_response['format']) 

str_result = result.get_prep_value() # returns a CloudinaryField string e.g. "image/upload/v123456789/test.png" 

# Save the result 
p = Photo(title="title",image=str_result) 
p.save() # save result in database