zee*_*kay 19
要检查模型上的字段,我通常使用?
:
>>> Person?
Type: ModelBase
Base Class: <class 'django.db.models.base.ModelBase'>
String Form: <class 'foo.bar.models.Person'>
Namespace: Interactive
File: /home/zk/ve/django/foo/bar/models.py
Docstring:
Person(id, first_name, last_name)
Run Code Online (Sandbox Code Playgroud)
你也可以使用help()
.如果您有模型的实例,您可以查看__dict__
:
>>> [x for x in Person().__dict__.keys() if not x.startswith('_')]
<<< ['first_name', 'last_name', 'id']
Run Code Online (Sandbox Code Playgroud)
如果你有模型实例,你可以简单地调用:
model_queryset.all().values()
Run Code Online (Sandbox Code Playgroud)
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values
对于使用模型类本身的字段列表,请参阅Django:获取模型字段列表?
或直接文档 - 对于Django 1.10,有一个关于如何使用字段的专用部分:https://docs.djangoproject.com/en/1.10/ref/models/meta/#retrieving-all-field-instances-of -一个模型
我想你只想__dict__
在一个模型的实例上使用.(虽然不会在ipython中提供像tab完成的方法).__doc__
通常使用也非常有帮助.
另请查看http://docs.python.org/library/inspect.html