python - Django 模型中可用的隐式字段有哪些?

标签 python django django-models django-orm

Django 为 models.py 中的所有模型提供了默认的 id 字段。是否还有其他字段以同样的方式提供?我在哪里可以获得 list ?

互联网上的搜索没有帮助我。

最佳答案

就数据库字段而言,只有id(也与pk相同),但就类字段而言,所有模型都将继承 Model 类,您可以在 django's source code on GitHub 上阅读其代码。了解它们的另一种快速方法是运行 django shell 并检查 dir 的内容。函数给你:

>>> from django.db import models
>>>
>>> class M(models.Model):
>>>     pass
>>>
>>> m = M()
>>> dir(m)

['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', '_base_manager', '_check_column_name_clashes',
'_check_field_name_clashes', '_check_fields', '_check_id_field', '_check_index_together',
'_check_local_fields', '_check_m2m_through_same_relationship', '_check_managers', '_check_model',
'_check_ordering', '_check_swappable', '_check_unique_together', '_default_manager',
'_deferred', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD',
'_get_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', 'date_error_message',
'delete', 'full_clean', 'id', 'objects', 'pk', 'prepare_database_save', 'save',
'save_base', 'serializable_value', 'unique_error_message', 'validate_unique']

请注意,这些是方法和字段。

关于python - Django 模型中可用的隐式字段有哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31981974/

相关文章:

python - 正则表达式查找 '\n'

django - 加载 Django 运行服务器提供的静态文件时如何修复 cors 错误

带有 2 个参数的 django url 模式

python - 无法修复 "zipimport.ZipImportError: can' t 解压数据; zlib 不可用”当我输入 "python3.6 get-pip.py"

用于 IntegerField 的 Django max_length

python - QtDesigner 将字体大小设置为-1

python - 在 Python 中计算结构的 CRC

python - 如何在 Django 中使用 <div class ='field_type'> 标记表单字段

python - 在 Cloud9 中升级 SAM CLI

python - Django休息框架: How serialize two models queryset?