前言
我的 App 項目的 API 部分是使用 Django REST Framework 來搭建的,它可以像搭積木一樣非常方便地搭出 API,兼具方便和靈活。
django是一個神奇的框架,而restframework又是遵循了這個框架的另一個神奇的框架,然而由于restframework的文檔稀爛無比,很多時候你必須看源碼才能寫出科學的代碼,這擋住了很多新手的路。
在使用的過程中我也積累了一些小技巧,這里寫一則關于如何為文件屬性輸出完整 URL 的字段。
實現方法
一個典型的案例是,當請求 /profile/ 這個 API 的時候,返回類似于這樣的結果:
{ "id": 1, "nickname": "管理員", "mobilephone": "1234567890", "avatar": "/media/profiles/2017/12/17/avatar.png"}在 Django REST 的定義中,我使用了自定義的一個擴展自 rest_framework.views.APIView 的 ProfileView 類型,實現了它的 get 方法,來給認證的用戶返回一個 Profile 對象:
class ProfileView(APIView): def get(self, request): user = request.user if user.is_authenticated: profile = Profile.objects.get(user=user) return Response(ProfileSerializer(profile).data) else: raise exceptions.AuthenticationFailed('Not authenticated user!')這里的邏輯很簡單,判斷請求當前 API 的用戶是不是已經驗證過的用戶,如果是的話,再得到它的 Profile,再通過 ProfileSerializer 把 profile 實例序列化成 JSON 對象。如果不是已驗證用戶,則會返回 401 驗證失敗相關信息。
以上輸出的內容,交給 Web 前端使用是沒什么問題的,但如果是給 App 使用,那么 avatar 這個文件屬性的相對 URL 不太合適,于是我們要改造一下這個 API,使其能輸出絕對 URL。
如何做呢?只需要將上面的 get 方法,稍加修改即可:
-class ProfileView(APIView):+class ProfileView(generics.GenericAPIView): parser_classes = (MultiPartParser, FormParser)+ serializer_class = ProfileSerializer def get(self, request): user = request.user if user.is_authenticated: profile = Profile.objects.get(user=user)- return Response(ProfileSerializer(profile).data)+ serializer = self.get_serializer(profile)+ return Response(serializer.data) else: raise exceptions.AuthenticationFailed('Not authenticated user!')不同于之前繼承自 APIView,現在繼承自 generics.GenericAPIView,這是一個更通用的類,可以看到,這里通過手動構建 ProfileSerializer 改成通過 self.get_serializer 來進行,這里有什么不同呢?
還得看看 Django REST 的源碼,GenericAPIView 這個類的 get_serializer 在做什么。
def get_serializer(self, *args, **kwargs): """ Return the serializer instance that should be used for validating and deserializing input, and for serializing output. """ serializer_class = self.get_serializer_class() kwargs['context'] = self.get_serializer_context() return serializer_class(*args, **kwargs)
|
新聞熱點
疑難解答