私は次の見解を持っています:
class Authenticate(generics.CreateAPIView):
serializer_class = AuthSerializer
def create(self, request):
serializer = AuthSerializer(request.POST)
# Do work here
これは、データがフォームとして渡される場合はうまく機能しますが、データが生の JSON として渡される場合、シリアライザーはすべてのフィールドが None に設定された状態でインスタンス化されます。ドキュメントには、生の JSON 引数の処理に固有のものがあるはずであると記載されています。
どんな助けでも大歓迎です。
アップデート
生の JSON を渡すときに Browsable API を期待どおりに動作させるために、次の回避策がありますが、もっと良い方法があるはずです。
def parse_data(request):
# If this key exists, it means that a raw JSON was passed via the Browsable API
if '_content' in request.POST:
stream = StringIO(request.POST['_content'])
return JSONParser().parse(stream)
return request.POST
class Authenticate(generics.CreateAPIView):
serializer_class = AuthSerializer
def create(self, request):
serializer = AuthSerializer(parse_data(request))
# Do work here