Search code examples
pythondjangopdbdjango-contenttypes

How to get a more detailed information on an object (a list of fields and methods) using Python's pdb?


I am using pdb (actually ipdb) to debug my Django models.py. In particular I am trying to debug these lines of code:

def add_can_view( sender, **kwargs ) :
    #import ipdb; ipdb.set_trace()
    for content_type in ContentType.objects.all():
        Permission.objects.create(
            content_type = content_type,
            codename     = 'view_{}'.format( content_type.model ),
            name         = 'Can view {}'.format( content_type.name )
        )

post_syncdb.connect( add_can_view )

The code above, which was provided in this question, has an IntegrityError error when I run python manage.py syncdb:

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (11, 0))
---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)
/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    173             else:
    174                 filename = fname
--> 175             __builtin__.execfile(filename, *where)

/Users/hobbes3/Sites/mysite/manage.py in <module>()
      8     from django.core.management import execute_from_command_line
      9 
---> 10     execute_from_command_line(sys.argv)

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/__init__.pyc in execute_from_command_line(argv)
    441     """
    442     utility = ManagementUtility(argv)
--> 443     utility.execute()
    444 
    445 def execute_manager(settings_mod, argv=None):

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/__init__.pyc in execute(self)
    380             sys.stdout.write(self.main_help_text() + '\n')
    381         else:
--> 382             self.fetch_command(subcommand).run_from_argv(self.argv)
    383 
    384 def setup_environ(settings_mod, original_settings_path=None):

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/base.pyc in run_from_argv(self, argv)
    194         options, args = parser.parse_args(argv[2:])
    195         handle_default_options(options)
--> 196         self.execute(*args, **options.__dict__)
    197 
    198     def execute(self, *args, **options):

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/base.pyc in execute(self, *args, **options)
    230             if self.requires_model_validation:
    231                 self.validate()
--> 232             output = self.handle(*args, **options)
    233             if output:
    234                 if self.output_transaction:

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/base.pyc in handle(self, *args, **options)
    369         if args:
    370             raise CommandError("Command doesn't accept any arguments")
--> 371         return self.handle_noargs(**options)
    372 
    373     def handle_noargs(self, **options):

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/South-0.7.4-py2.7.egg/south/management/commands/syncdb.pyc in handle_noargs(self, migrate_all, **options)
     88 
     89         # OK, run the actual syncdb

---> 90         syncdb.Command().execute(**options)
     91 
     92         settings.INSTALLED_APPS = old_installed

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/base.pyc in execute(self, *args, **options)
    230             if self.requires_model_validation:
    231                 self.validate()
--> 232             output = self.handle(*args, **options)
    233             if output:
    234                 if self.output_transaction:

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/base.pyc in handle(self, *args, **options)
    369         if args:
    370             raise CommandError("Command doesn't accept any arguments")
--> 371         return self.handle_noargs(**options)
    372 
    373     def handle_noargs(self, **options):

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/commands/syncdb.pyc in handle_noargs(self, **options)
    108         # Send the post_syncdb signal, so individual apps can do whatever they need

    109         # to do at this point.

--> 110         emit_post_sync_signal(created_models, verbosity, interactive, db)
    111 
    112         # The connection may have been closed by a syncdb handler.


/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/sql.pyc in emit_post_sync_signal(created_models, verbosity, interactive, db)
    187         models.signals.post_syncdb.send(sender=app, app=app,
    188             created_models=created_models, verbosity=verbosity,
--> 189             interactive=interactive, db=db)

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/dispatch/dispatcher.pyc in send(self, sender, **named)
    170 
    171         for receiver in self._live_receivers(_make_id(sender)):
--> 172             response = receiver(signal=self, sender=sender, **named)
    173             responses.append((receiver, response))
    174         return responses

/Users/hobbes3/Sites/mysite/doors/signals.py in add_can_view(sender, **kwargs)
      8             content_type = content_type,
      9             codename     = 'view_{}'.format( content_type.model ),
---> 10             name         = 'Can view {}'.format( content_type.name )
     11         )
     12 

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/manager.pyc in create(self, **kwargs)
    135 
    136     def create(self, **kwargs):
--> 137         return self.get_query_set().create(**kwargs)
    138 
    139     def bulk_create(self, *args, **kwargs):

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/query.pyc in create(self, **kwargs)
    375         obj = self.model(**kwargs)
    376         self._for_write = True
--> 377         obj.save(force_insert=True, using=self.db)
    378         return obj
    379 

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/base.pyc in save(self, force_insert, force_update, using)
    461         if force_insert and force_update:
    462             raise ValueError("Cannot force both insert and updating in model saving.")
--> 463         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
    464 
    465     save.alters_data = True

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/base.pyc in save_base(self, raw, cls, origin, force_insert, force_update, using)
    549 
    550                 update_pk = bool(meta.has_auto_field and not pk_set)
--> 551                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
    552 
    553                 if update_pk:

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/manager.pyc in _insert(self, objs, fields, **kwargs)
    201 
    202     def _insert(self, objs, fields, **kwargs):
--> 203         return insert_query(self.model, objs, fields, **kwargs)
    204 
    205     def _update(self, values, **kwargs):

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/query.pyc in insert_query(model, objs, fields, return_id, raw, using)
   1574     query = sql.InsertQuery(model)
   1575     query.insert_values(fields, objs, raw=raw)
-> 1576     return query.get_compiler(using=using).execute_sql(return_id)
   1577 
   1578 

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in execute_sql(self, return_id)
    908         cursor = self.connection.cursor()
    909         for sql, params in self.as_sql():
--> 910             cursor.execute(sql, params)
    911         if not (return_id and cursor):
    912             return

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/backends/util.pyc in execute(self, sql, params)
     38         start = time()
     39         try:
---> 40             return self.cursor.execute(sql, params)
     41         finally:
     42             stop = time()

/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.pyc in execute(self, query, args)
     50     def execute(self, query, args=None):
     51         try:
---> 52             return self.cursor.execute(query, args)
     53         except Database.IntegrityError, e:
     54             raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]

IntegrityError: duplicate key value violates unique constraint "auth_permission_content_type_id_codename_key"
DETAIL:  Key (content_type_id, codename)=(2, view_group) already exists.

I have a feeling that somehow there is a duplicate content_type and the error occurs when I try to apply the permission twice to content_type_id 2, which is auth.group. So when after I uncomment the import ipdb line, I want to check out some objects such as content_type, but if I type that, then all I get is <ContentType: content type>.

I know I can simply go look at the documentation to see the model, but how would you get a list of fields and methods without knowing what the class is?

Also bonus points for solving this error lol.


Solution

  • Use dir (e.g. dir(content_type)) to get a list of attributes (including methods).

    In relation to django models, there is also a lot of information inside the _meta property. Explore that with dir(my_model_object._meta).