gpt4 book ai didi

python - 如何使用 Python 的 pdb 获取有关对象的更详细信息(字段和方法列表)?

转载 作者:行者123 更新时间:2023-11-28 21:27:46 24 4
gpt4 key购买 nike

我正在使用 pdb (实际上是 ipdb )调试我的 Django models.py .特别是我正在尝试调试这些代码行:

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 )

上面的代码,在this question中提供, 有一个 IntegrityError运行时出错 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.

我有一种感觉,不知何故有一个重复的 content_type当我尝试两次将权限应用到 content_type_id 2 时发生错误,即 auth.group .所以当我取消注释 import ipdb 之后行,我想检查一些对象,例如 content_type , 但如果我输入那个,那么我得到的就是 <ContentType: content type> .

我知道我可以直接去看看 documentation查看模型,但是在不知道类是什么的情况下如何获得字段和方法的列表?

还有解决这个错误的奖励积分哈哈。

最佳答案

使用 dir(例如 dir(content_type))获取属性列表(包括方法)。

关于django模型,_meta属性里面也有很多信息。使用 dir(my_model_object._meta) 探索它。

关于python - 如何使用 Python 的 pdb 获取有关对象的更详细信息(字段和方法列表)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9979467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com