Django REST Framework動作確認 デバッガー設定

programming-gogogogo.hatenablog.com

上記の記事でViewに関数を定義して、APIリクエストに対しModelのデータを返すことまでできたところからの続きです。

一度デバッガーを動かしてデータの中身を確認したいので、デバッガーが動作するよう設定します。

VSCodeのデバッガーのアイコンをクリックして'create a launch.json file'をクリックします。

Djangoを選択してクリックします。

manage.pyがあるパスを指定してEnterを押します。

以下のlaunch.jsonが作成されました。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/drfproject/manage.py",
            "args": [
                "runserver"
            ],
            "django": true,
            "justMyCode": true
        }
    ]
}

justMyCodeがtrueだとライブラリの中身に入れなくなるようなので、falseにしておきます。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/drfproject/manage.py",
            "args": [
                "runserver"
            ],
            "django": true,
            "justMyCode": false
        }
    ]
}

そしてmanage.pyなどのファイルにフォーカスしてからF5ボタンを押すとデバッガーが起動しました。

drfproject/blog/views.pyのusers_list関数内にブレークポイントを設定してみます。

新規ターミナルを開き、APIリクエストを送ります。

curl http://localhost:8000/blog/

users_list関数のブレークポイントで処理が止まりました。

1行進んでみると、User.objects.all()の処理で変数users<QuerySet[<User: User object (1)>]>が代入されていました。

あと1行進むと変数serializerUserSerializer(<QuerySet[<User: User object(1)>]>, many=True)が代入されていました。

WATCHでserializer.dataを見てみるとOrderedDict形式でモデルのデータが入っているのがわかります。

なんとなく処理の流れは見えてきたので自分の理解が曖昧なQuerySetについて調べてみます。

ちょっと調べた感じ、QuerySetはDjango REST Framework特有の概念ではなくDjangoでも使われているようなので、Djangoの公式ドキュメントを読むのが良さそう。

QuerySet API referenceというのがありました。 https://docs.djangoproject.com/en/4.1/ref/models/querysets/

This document describes the details of the QuerySet API. It builds on the material presented in the model and database query guides, so you’ll probably want to read and understand those documents before reading this one.

Throughout this reference we’ll use the example blog models presented in the database query guide.

上記を読む限り、QuerySetのAPI referenceを読む前にDjangoのmodelやdatabase queryのガイドを読んだ方が良さそうでした。

Modelのドキュメント https://docs.djangoproject.com/en/dev/topics/db/models/

database queryのドキュメント https://docs.djangoproject.com/en/dev/topics/db/queries/

これらのドキュメントは別で読もうと思うのですが、先にQuerySetとは何かを説明している文章も探してみたら以下がわかりやすかったです。 https://www.w3schools.com/django/django_queryset.php#:~:text=A%20QuerySet%20is%20a%20collection,data%20from%20the%20Member%20table.

A QuerySet is a collection of data from a database.

A QuerySet is built up as a list of objects.

QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data at an early stage.

QuerySetはDBから取得したデータの集合で、オブジェクトのリストで形成されている。QuerySetsがあることでフィルターや順序づけて必要なデータを取得しやすくなる。という感じで解釈できそうです。