ではない私は、次のページネーションとのリストについては、DjangoのAPIを作成しようとしているが、このエラーを持っているページネーションとDjangoのAPIリストに - ページはJSONシリアライズ可能な
TypeError: Object of type 'Page' is not JSON serializable
ています私のAPIコードです:
@api_view(['POST'])
def employee_get_list_by_page(request):
# ----- YAML below for Swagger -----
"""
description: employee_get_list_by_page
parameters:
- name: token
type: string
required: true
location: form
- name: page
type: string
required: true
location: form
- name: page_limit
type: string
required: true
location: form
"""
token = request.POST['token']
try:
auth_employee = AuthEmployeeSessionToken.objects.get(token=token)
except AuthEmployeeSessionToken.DoesNotExist:
return Response("Invalid Token", status=status.HTTP_406_NOT_ACCEPTABLE)
employee_list = Employee.objects.filter(company = auth_employee.employee.company.id)
page = request.GET.get('page', request.POST['page'])
paginator = Paginator(employee_list, request.POST['page_limit'])
try:
employees = paginator.page(page)
except PageNotAnInteger:
employees = paginator.page(request.POST['page'])
except EmptyPage:
employees = paginator.page(paginator.num_pages)
return Response(employees,status=status.HTTP_200_OK) <-- passing employees probably cause this error that employees as Page is not JSON serializable.
これは私のモデルhttps://gist.github.com/axilaris/89b2ac6a7762f428ad715f4916f43967です。 as_dict notice私は応答のために私のjsonを作成するこの.as_dict()を持っています。私は単一の要求のためにそれを使用するが、どのように行われているかわからないリストのために。
- ページングをサポートしているこのリストのAPIクエリはどのようにして作成されますか?
- JSONに必要な項目だけをリストしたいという面倒な問題。
私は質問に合格できません。ここにエラーがありますhttps://gist.github.com/axilaris/226ad1e0dde14d96b1be6eb71ddd6e88 – Axil
もう1つのリファレンスを追加しましたhttps://gist.github.com/axilaris/89b2ac6a7762f428ad715f4916f43967私のモデルを設計して、私が望むようなjsonフィールドを表示する方法について。そこから使用できるものは何ですか? – Axil
ねえ、その問題を注文で解決するために私の答えを更新しました。あなたのモデルに基づいて私はあなたのために私の答えを少し広げます。 – ARJMP