Debugging backend #2467
szabozoltan69
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Besides the great Sentry issues sometimes it is useful to debug backend processes.
One option is the evergreen
pdb
, via a simpleimport pdb; pdb.set_trace()
breakpoint in any def-s or locations (which is not a class definition and where program control can be stopped). With simple commands wonders can be done. One of the most useful ones is thepp vars(...)
listing of object properties.Sometimes it is useful to debug in an IDE. PyCharm has very sophisticated options, but mainly in the paid version.
So I tried to fire up the VS Code debugging. It was not so straightforward as it seemed to (or it has to be), but finally it succeeded.
In case of a non-docker local run, the defaults are fine, "Run and Debug" blue button (in the Debug left section), then choose Django, then manage.py. Via
ps aux|grep debugpy
it turned out that quite a complex system is fired up (4 processes can be seen for the simple "listening").But the dockerised solution needed some luck to work. I had to install the debugpy package (
debugpy==1.8.14
) and remove thetyping
package (which is not any more needed above python 3.11) viapyproject.toml
. The "on the fly, in-container install and remove" did not succeed. Also DebugToolbar is something to be removed for now, due to sometimes it causes double runs.I had to add 5678 port to the exposed ones, so in docker-compose.yml I had:
and instead of the
command: python manage.py runserver 0.0.0.0:8000
I could use:command: python -Xfrozen_modules=off -m debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000
The
-Xfrozen_modules=off
is crucial, otherwise a lot of annoying issues come up.In the .vscode subdir (of go-api) I have these files:
settings.json
and the most important one,
launch.json
:The strange is that
host
andport
is underlined as erroneous, but it works fine (and I did not find any other better solution).This way I could insert breakpoints and after pushing the "green play triangle" I could have the desired functionality:

Let's see a practical troubleshooting.
In case of a
curl 'http://localhost:8000/api/v2/country-plan/81/'
request the CountryPlanViewset gives the response. Unfortunately there is no "def" there in drf_views, only the class itself. But we can insert amethod there, and can set the breakpoint to
pass
. If the run pauses there, we can requeststr(self.queryset.query)
– the str() is important, otherwise an overcomplicated structure is to be struggled with – and this way we can check the SQL query that is generated from the default queryset:SELECT "country_plan_countryplan"."created_by_id", "country_plan_countryplan"."created_at", "country_plan_countryplan"."updated_by_id", "country_plan_countryplan"."updated_at", "country_plan_countryplan"."country_id", "country_plan_countryplan"."internal_plan_file", "country_plan_countryplan"."internal_plan_url", "country_plan_countryplan"."public_plan_file", "country_plan_countryplan"."public_plan_url", "country_plan_countryplan"."requested_amount", "country_plan_countryplan"."people_targeted", "country_plan_countryplan"."is_publish", "country_plan_countryplan"."public_plan_inserted_date", "country_plan_countryplan"."internal_plan_inserted_date" FROM "country_plan_countryplan" WHERE "country_plan_countryplan"."is_publish"
Or, if you want to dive deeper, you can put a breakpoint to
def retrieve
(in the viewset class) or todef to_representation
(in the serializer class). This way, debugging the "instance" it is possible to have a look at the prefetched or annotated entities, e.g. viastr(instance.country_plan_sp.all().query)
andstr(instance.country_plan_mc.all().query)
.E.g. this way:
If there is a
def get_queryset(self)
, the following approach can be followed:One more step is to use
from django.db import connection
, then inget_queryset
it is possible use aprint(connection.queries)
, which gives a lot of queries with prefetch_related.I've pushed the changes into vscode branch.
Beta Was this translation helpful? Give feedback.
All reactions