Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging a custom django management command

Tags:

django

I can't find how to debug custom management commands.

When errors happen, they just say something like :

IndexError: list index out of range

How to get more debugging info ?

Like the number of the line where it crashes for instance, that would be helpful.

like image 880
Adrien Avatar asked Jul 16 '13 16:07

Adrien


2 Answers

There is a traceback option that does the trick :

python manage.py command_name --traceback

The command then outputs usual python errors

like image 162
Adrien Avatar answered Oct 17 '22 06:10

Adrien


use -i option and pdb:

python -i manage.py command_name

failed or not , you will have a python repl, so you can use pdb to jump into the stacks raising exception:

    return executor(sql, params, many, context)
  File "/home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute 
    return self.cursor.execute(sql, params)
  File "/home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__          
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute 
    return self.cursor.execute(sql, params)                                                                                    
django.db.utils.DataError: value too long for type character varying(10)                                                       

>>> import pdb                                                                                                                 
>>> pdb.pm()                                                                                                                   
> /home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/backends/utils.py(84)_execute()                 
-> return self.cursor.execute(sql, params)                                                                                                                   
(Pdb) param  
blablabalablabalablbalbalabla

like image 28
Gary 612 Avatar answered Oct 17 '22 05:10

Gary 612