Django拾遗之1

Django通过表,取到表所以在的APP的名字

F:\python从入门到放弃\实战\11.22\PerfectCRM>python manage.py shell
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from crm import models
>>> models.UserProfile
<class 'crm.models.UserProfile'>
>>> models.UserProfile._meta
<Options for UserProfile>
>>> dir(models.UserProfile._meta)
['FORWARD_PROPERTIES', 'REVERSE_PROPERTIES', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_
subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_expire_cache',
'_forward_fields_map', '_get_fields', '_get_fields_cache', '_ordering_clash', '_populate_directed_relation_graph', '_prepare', '_property_names', '_relation_tree', 'abstract', 'add_field', 'add_manager',
 'app_config', 'app_label', 'apps', 'auto_created', 'auto_field', 'base_manager', 'base_manager_name', 'can_migrate', 'concrete_fields', 'concrete_model', 'contribute_to_class', 'db_table', 'db_tablespac
e', 'default_apps', 'default_manager', 'default_manager_name', 'default_permissions', 'default_related_name', 'fields', 'fields_map', 'get_ancestor_link', 'get_base_chain', 'get_field', 'get_fields', 'ge
t_latest_by', 'get_parent_list', 'get_path_from_parent', 'get_path_to_parent', 'index_together', 'indexes', 'installed', 'label', 'label_lower', 'local_concrete_fields', 'local_fields', 'local_managers',
 'local_many_to_many', 'managed', 'managers', 'managers_map', 'many_to_many', 'model', 'model_name', 'object_name', 'order_with_respect_to', 'ordering', 'original_attrs', 'parents', 'permissions', 'pk',
'private_fields', 'proxy', 'proxy_for_model', 'related_fkey_lookups', 'related_objects', 'required_db_features', 'required_db_vendor', 'select_on_save', 'setup_pk', 'setup_proxy', 'swappable', 'swapped',
 'unique_together', 'verbose_name', 'verbose_name_plural', 'verbose_name_raw']
>>> models.UserProfile._meta.app_config
<CrmConfig: crm>
>>> models.UserProfile._meta.label
'crm.UserProfile'
>>> models.UserProfile._meta.app_label
'crm'
>>>
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

F:\python从入门到放弃\实战\11.22\PerfectCRM>dir
 驱动器 F 中的卷没有标签。
 卷的序列号是 0007-4A5F

 F:\python从入门到放弃\实战\11.22\PerfectCRM 的目录

2018/12/04  13:51    <DIR>          .
2018/12/04  13:51    <DIR>          ..
2018/12/05  09:44    <DIR>          .idea
2018/12/04  13:48    <DIR>          crm
2018/12/04  17:17    <DIR>          king_admin
2018/11/22  15:44               557 manage.py
2018/12/04  14:18    <DIR>          PerfectCRM
2018/11/23  14:29    <DIR>          statics
2018/11/23  16:53    <DIR>          student
2018/12/04  14:17    <DIR>          templates
               1 个文件            557 字节
               9 个目录 267,912,523,776 可用字节

F:\python从入门到放弃\实战\11.22\PerfectCRM>python manage.py shell
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from crm import shell
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name 'shell'
>>> from crm import models
>>> models.UserProfile._meta.app_label
'crm'
>>> models.UserProfile._meta.verbose_name_plural
'user profiles'
>>> models.Customer._meta.verbose_name_plural
'客户表'
>>> models.Customer._meta.verbose_name
'客户表'
>>>
>>>
>>>
>>>
>>> dir(importlib)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'importlib' is not defined
>>> import importlib
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_
bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches', 'machinery', 'reload', 'sys', 'type
s', 'util', 'warnings']
>>>
>>> importlib.import_module('crm')
<module 'crm' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\__init__.py'>
>>> importlib.import_module('crm.models')
<module 'crm.models' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\models.py'>
>>> m=importlib.import_module('crm.models')
>>> m
<module 'crm.models' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\models.py'>
>>> m.UserProfile
<class 'crm.models.UserProfile'>
>>> dir(m)
['Branch', 'ClassList', 'Course', 'CourseRecord', 'Customer', 'CustomerFollowUp', 'Enrollment', 'Menu', 'Payment', 'Role', 'StudyRecord', 'Tag', 'User', 'UserPr
ofile', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'models']
>>> dir(m.models)
['Aggregate', 'AutoField', 'Avg', 'BLANK_CHOICE_DASH', 'BigAutoField', 'BigIntegerField', 'BinaryField', 'BooleanField', 'CASCADE', 'Case', 'CharField', 'CommaS
eparatedIntegerField', 'Count', 'DEFERRED', 'DO_NOTHING', 'DateField', 'DateTimeField', 'DecimalField', 'DurationField', 'EmailField', 'Empty', 'Exists', 'Expre
ssion', 'ExpressionList', 'ExpressionWrapper', 'F', 'Field', 'FieldDoesNotExist', 'FileField', 'FilePathField', 'FilteredRelation', 'FloatField', 'ForeignKey',
'ForeignObject', 'Func', 'GenericIPAddressField', 'IPAddressField', 'ImageField', 'Index', 'IntegerField', 'Lookup', 'Manager', 'ManyToManyField', 'ManyToManyRe
l', 'ManyToOneRel', 'Max', 'Min', 'Model', 'NOT_PROVIDED', 'NullBooleanField', 'ObjectDoesNotExist', 'OneToOneField', 'OneToOneRel', 'OrderWrt', 'OuterRef', 'PR
OTECT', 'PositiveIntegerField', 'PositiveSmallIntegerField', 'Prefetch', 'ProtectedError', 'Q', 'QuerySet', 'RowRange', 'SET', 'SET_DEFAULT', 'SET_NULL', 'SlugF
ield', 'SmallIntegerField', 'StdDev', 'Subquery', 'Sum', 'TextField', 'TimeField', 'Transform', 'URLField', 'UUIDField', 'Value', 'ValueRange', 'Variance', 'Whe
n', 'Window', 'WindowFrame', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'a
ggregates', 'aggregates_all', 'base', 'constants', 'deletion', 'expressions', 'fields', 'fields_all', 'functions', 'indexes', 'indexes_all', 'lookups', 'manager
', 'options', 'prefetch_related_objects', 'query', 'query_utils', 'signals', 'sql', 'utils']
>>> m
<module 'crm.models' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\models.py'>
>>> m.Customer.objects.all()[1]
<Customer: qrqewrqe>
>>> m.Customer.objects.all()[0]
<Customer: 12313123>
>>> m.Customer.objects.all()[0].qq
'12313123'
>>> c=m.Customer.objects.all()[1]
>>> c
<Customer: qrqewrqe>
>>> c.source
2
>>> c._meta.fields
(<django.db.models.fields.AutoField: id>, <django.db.models.fields.CharField: name>, <django.db.models.fields.CharField: qq>, <django.db.models.fields.CharField
: qqname>, <django.db.models.fields.CharField: phone>, <django.db.models.fields.SmallIntegerField: source>, <django.db.models.fields.CharField: referral_from>,
<django.db.models.fields.related.ForeignKey: consult_course>, <django.db.models.fields.TextField: content>, <django.db.models.fields.related.ForeignKey: consult
ant>, <django.db.models.fields.TextField: memo>, <django.db.models.fields.CharField: status>, <django.db.models.fields.DateTimeField: date>)
>>> c._meta.get_field('source')
<django.db.models.fields.SmallIntegerField: source>
>>> s1=c._meta.get_field('source')
>>> dir(s1)
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__ha
sh__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size
of__', '__str__', '__subclasshook__', '__weakref__', '_check_backend_specific_checks', '_check_choices', '_check_db_index', '_check_deprecation_details', '_chec
k_field_name', '_check_max_length_warning', '_check_null_allowed_for_primary_keys', '_check_validators', '_clear_cached_lookups', '_db_tablespace', '_descriptio
n', '_error_messages', '_get_default', '_get_flatchoices', '_get_lookup', '_unique', '_unregister_lookup', '_validators', '_verbose_name', 'attname', 'auto_crea
ted', 'auto_creation_counter', 'blank', 'cached_col', 'cast_db_type', 'check', 'choices', 'class_lookups', 'clean', 'clone', 'column', 'concrete', 'contribute_t
o_class', 'creation_counter', 'db_check', 'db_column', 'db_index', 'db_parameters', 'db_tablespace', 'db_type', 'db_type_parameters', 'db_type_suffix', 'deconst
ruct', 'default', 'default_error_messages', 'default_validators', 'description', 'editable', 'empty_strings_allowed', 'empty_values', 'error_messages', 'flatcho
ices', 'formfield', 'get_attname', 'get_attname_column', 'get_choices', 'get_col', 'get_db_converters', 'get_db_prep_save', 'get_db_prep_value', 'get_default',
'get_filter_kwargs_for_object', 'get_internal_type', 'get_lookup', 'get_lookups', 'get_pk_value_on_save', 'get_prep_value', 'get_transform', 'has_default', 'hel
p_text', 'hidden', 'is_relation', 'many_to_many', 'many_to_one', 'max_length', 'merge_dicts', 'model', 'name', 'null', 'one_to_many', 'one_to_one', 'pre_save',
'primary_key', 'register_lookup', 'rel_db_type', 'related_model', 'remote_field', 'run_validators', 'save_form_data', 'select_format', 'serialize', 'set_attribu
tes_from_name', 'system_check_deprecated_details', 'system_check_removed_details', 'to_python', 'unique', 'unique_for_date', 'unique_for_month', 'unique_for_yea
r', 'validate', 'validators', 'value_from_object', 'value_to_string', 'verbose_name']
>>> s1.verbose_name
'source'
>>>
View Code

 


Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

F:\python从入门到放弃\实战\11.22\PerfectCRM>dir
 驱动器 F 中的卷没有标签。
 卷的序列号是 0007-4A5F

 F:\python从入门到放弃\实战\11.22\PerfectCRM 的目录

2018/12/04  13:51    <DIR>          .
2018/12/04  13:51    <DIR>          ..
2018/12/05  09:44    <DIR>          .idea
2018/12/04  13:48    <DIR>          crm
2018/12/04  17:17    <DIR>          king_admin
2018/11/22  15:44               557 manage.py
2018/12/04  14:18    <DIR>          PerfectCRM
2018/11/23  14:29    <DIR>          statics
2018/11/23  16:53    <DIR>          student
2018/12/04  14:17    <DIR>          templates
               1 个文件            557 字节
               9 个目录 267,912,523,776 可用字节

F:\python从入门到放弃\实战\11.22\PerfectCRM>python manage.py shell
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from crm import shell
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name 'shell'
>>> from crm import models
>>> models.UserProfile._meta.app_label
'crm'
>>> models.UserProfile._meta.verbose_name_plural
'user profiles'
>>> models.Customer._meta.verbose_name_plural
'客户表'
>>> models.Customer._meta.verbose_name
'客户表'
>>>
>>>
>>>
>>>
>>> dir(importlib)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'importlib' is not defined
>>> import importlib
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_
bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches', 'machinery', 'reload', 'sys', 'type
s', 'util', 'warnings']
>>>
>>> importlib.import_module('crm')
<module 'crm' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\__init__.py'>
>>> importlib.import_module('crm.models')
<module 'crm.models' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\models.py'>
>>> m=importlib.import_module('crm.models')
>>> m
<module 'crm.models' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\models.py'>
>>> m.UserProfile
<class 'crm.models.UserProfile'>
>>> dir(m)
['Branch', 'ClassList', 'Course', 'CourseRecord', 'Customer', 'CustomerFollowUp', 'Enrollment', 'Menu', 'Payment', 'Role', 'StudyRecord', 'Tag', 'User', 'UserPr
ofile', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'models']
>>> dir(m.models)
['Aggregate', 'AutoField', 'Avg', 'BLANK_CHOICE_DASH', 'BigAutoField', 'BigIntegerField', 'BinaryField', 'BooleanField', 'CASCADE', 'Case', 'CharField', 'CommaS
eparatedIntegerField', 'Count', 'DEFERRED', 'DO_NOTHING', 'DateField', 'DateTimeField', 'DecimalField', 'DurationField', 'EmailField', 'Empty', 'Exists', 'Expre
ssion', 'ExpressionList', 'ExpressionWrapper', 'F', 'Field', 'FieldDoesNotExist', 'FileField', 'FilePathField', 'FilteredRelation', 'FloatField', 'ForeignKey',
'ForeignObject', 'Func', 'GenericIPAddressField', 'IPAddressField', 'ImageField', 'Index', 'IntegerField', 'Lookup', 'Manager', 'ManyToManyField', 'ManyToManyRe
l', 'ManyToOneRel', 'Max', 'Min', 'Model', 'NOT_PROVIDED', 'NullBooleanField', 'ObjectDoesNotExist', 'OneToOneField', 'OneToOneRel', 'OrderWrt', 'OuterRef', 'PR
OTECT', 'PositiveIntegerField', 'PositiveSmallIntegerField', 'Prefetch', 'ProtectedError', 'Q', 'QuerySet', 'RowRange', 'SET', 'SET_DEFAULT', 'SET_NULL', 'SlugF
ield', 'SmallIntegerField', 'StdDev', 'Subquery', 'Sum', 'TextField', 'TimeField', 'Transform', 'URLField', 'UUIDField', 'Value', 'ValueRange', 'Variance', 'Whe
n', 'Window', 'WindowFrame', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'a
ggregates', 'aggregates_all', 'base', 'constants', 'deletion', 'expressions', 'fields', 'fields_all', 'functions', 'indexes', 'indexes_all', 'lookups', 'manager
', 'options', 'prefetch_related_objects', 'query', 'query_utils', 'signals', 'sql', 'utils']
>>> m
<module 'crm.models' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\models.py'>
>>> m.Customer.objects.all()[1]
<Customer: qrqewrqe>
>>> m.Customer.objects.all()[0]
<Customer: 12313123>
>>> m.Customer.objects.all()[0].qq
'12313123'
>>> c=m.Customer.objects.all()[1]
>>> c
<Customer: qrqewrqe>
>>> c.source
2
>>> c._meta.fields
(<django.db.models.fields.AutoField: id>, <django.db.models.fields.CharField: name>, <django.db.models.fields.CharField: qq>, <django.db.models.fields.CharField
: qqname>, <django.db.models.fields.CharField: phone>, <django.db.models.fields.SmallIntegerField: source>, <django.db.models.fields.CharField: referral_from>,
<django.db.models.fields.related.ForeignKey: consult_course>, <django.db.models.fields.TextField: content>, <django.db.models.fields.related.ForeignKey: consult
ant>, <django.db.models.fields.TextField: memo>, <django.db.models.fields.CharField: status>, <django.db.models.fields.DateTimeField: date>)
>>> c._meta.get_field('source')
<django.db.models.fields.SmallIntegerField: source>
>>> s1=c._meta.get_field('source')
>>> dir(s1)
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__ha
sh__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size
of__', '__str__', '__subclasshook__', '__weakref__', '_check_backend_specific_checks', '_check_choices', '_check_db_index', '_check_deprecation_details', '_chec
k_field_name', '_check_max_length_warning', '_check_null_allowed_for_primary_keys', '_check_validators', '_clear_cached_lookups', '_db_tablespace', '_descriptio
n', '_error_messages', '_get_default', '_get_flatchoices', '_get_lookup', '_unique', '_unregister_lookup', '_validators', '_verbose_name', 'attname', 'auto_crea
ted', 'auto_creation_counter', 'blank', 'cached_col', 'cast_db_type', 'check', 'choices', 'class_lookups', 'clean', 'clone', 'column', 'concrete', 'contribute_t
o_class', 'creation_counter', 'db_check', 'db_column', 'db_index', 'db_parameters', 'db_tablespace', 'db_type', 'db_type_parameters', 'db_type_suffix', 'deconst
ruct', 'default', 'default_error_messages', 'default_validators', 'description', 'editable', 'empty_strings_allowed', 'empty_values', 'error_messages', 'flatcho
ices', 'formfield', 'get_attname', 'get_attname_column', 'get_choices', 'get_col', 'get_db_converters', 'get_db_prep_save', 'get_db_prep_value', 'get_default',
'get_filter_kwargs_for_object', 'get_internal_type', 'get_lookup', 'get_lookups', 'get_pk_value_on_save', 'get_prep_value', 'get_transform', 'has_default', 'hel
p_text', 'hidden', 'is_relation', 'many_to_many', 'many_to_one', 'max_length', 'merge_dicts', 'model', 'name', 'null', 'one_to_many', 'one_to_one', 'pre_save',
'primary_key', 'register_lookup', 'rel_db_type', 'related_model', 'remote_field', 'run_validators', 'save_form_data', 'select_format', 'serialize', 'set_attribu
tes_from_name', 'system_check_deprecated_details', 'system_check_removed_details', 'to_python', 'unique', 'unique_for_date', 'unique_for_month', 'unique_for_yea
r', 'validate', 'validators', 'value_from_object', 'value_to_string', 'verbose_name']
>>> s1.verbose_name
'source'
>>> s1.choices
((0, '转介绍'), (1, 'QQ群'), (2, '官网'), (3, '百度推广'), (4, '51CTU'), (5, '知乎'), (6, '市场推广'))
>>>
>>>
>>> s1._check_choices
<bound method Field._check_choices of <django.db.models.fields.SmallIntegerField: source>>
>>> s1.default
<class 'django.db.models.fields.NOT_PROVIDED'>
>>> s1.get_choices()
[('', '---------'), (0, '转介绍'), (1, 'QQ群'), (2, '官网'), (3, '百度推广'), (4, '51CTU'), (5, '知乎'), (6, '市场推广')]
>>>
>>>
>>> getattr(c,'get_soure_display')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Customer' object has no attribute 'get_soure_display'
>>> getattr(c,'get_source_display')
functools.partial(<bound method Model._get_FIELD_display of <Customer: qrqewrqe>>, field=<django.db.models.fields.SmallIntegerField: source>)
>>> getattr(c,'get_source_display')()
'官网'
>>>
View Code

 

 

>>> getattr(c,'get_source_display')()
'官网'
>>> dir(c)
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__
repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_column_name_clashes', '_check_field_name_clashes', '
_check_fields', '_check_id_field', '_check_index_together', '_check_indexes', '_check_local_fields', '_check_long_column_names', '_check_m2m_through_same_relati
onship', '_check_managers', '_check_model', '_check_model_name_db_lookup_clashes', '_check_ordering', '_check_property_name_related_field_accessor_clashes', '_c
heck_single_primary_key', '_check_swappable', '_check_unique_together', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_ge
t_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_save_parents', '_save_table', '_
set_pk_val', '_state', 'check', 'clean', 'clean_fields', 'consult_course', 'consult_course_id', 'consultant', 'consultant_id', 'content', 'customerfollowup_set'
, 'date', 'date_error_message', 'delete', 'enrollment_set', 'from_db', 'full_clean', 'get_deferred_fields', 'get_next_by_date', 'get_previous_by_date', 'get_sou
rce_display', 'get_status_display', 'id', 'memo', 'name', 'objects', 'payment_set', 'phone', 'pk', 'prepare_database_save', 'qq', 'qqname', 'referral_from', 're
fresh_from_db', 'save', 'save_base', 'serializable_value', 'source', 'source_choices', 'status', 'status_choice', 'tags', 'unique_error_message', 'validate_uniq
ue']
>>> 
>>> getattr(c,'get_status_display')()
'已报名'
>>> getattr(c,'get_next_by_date')()
<Customer: iiiiii>
>>> getattr(c,'get_previous_by_date')()
<Customer: 12313123>
>>>

  

 

F:\python从入门到放弃\实战\11.22\PerfectCRM>python manage.py shell
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import importlib
>>> m=importlib.import_module('crm.models')
>>> m
<module 'crm.models' from 'F:\\python从入门到放弃\\实战\\11.22\\PerfectCRM\\crm\\models.py'>
>>> c=m.Customer.objects.all()[0]
>>> c
<Customer: 12313123>
>>> getattr(c,'get_soure_display')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Customer' object has no attribute 'get_soure_display'
>>> getattr(c,'get_source_display')
functools.partial(<bound method Model._get_FIELD_display of <Customer: 12313123>>, field=<django.db.models.fields.SmallIntegerField: source>)
>>> getattr(c,'get_source_display')()
'QQ群'
>>> n=getattr(c,'get_source_display')()
>>> n
'QQ群'
>>> type(n)
<class 'str'>
>>> type(n).__name__
'str'
>>>

 

 

 

 

 

 

 

  

 

posted @ 2018-12-04 14:52  巨兽~墨菲特  阅读(169)  评论(0编辑  收藏  举报