More Related Content
Storytelling For The Web: Integrate Storytelling in your Design Process Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis... How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR... 2024 Trend Updates: What Really Works In SEO & Content Marketing How to manipulate data in databases using Django Model API 2024 State of Marketing Report – by Hubspot Everything You Need To Know About ChatGPT Product Design Trends in 2024 | Teenage Engineerings Recently uploaded (20)
黑客技术,安全提分不是梦!我们采用最新的数据破解和隐藏技术,精准定位并修改你的成绩,同时采用深度隐藏技术确保你的操作不被发现。价格实惠,流程快速,事后无痕... 3分钟读懂佩珀代因大学毕业证Pepperdine毕业证学历认证 3分钟读懂贝尔法斯特女王大学毕业证QUB毕业证学历认证 黑客出手,分数我有!安全可靠的技术支持,让你的GPA瞬间提升,留学之路更加顺畅!【微信:viphuzhao】 3分钟读懂索尔福德大学毕业证Salford毕业证学历认证 3分钟读懂纽曼大学毕业证Newman毕业证学历认证 3分钟读懂曼彻斯特城市大学毕业证MMU毕业证学历认证 3分钟读懂利物浦约翰摩尔大学毕业证LJMU毕业证学历认证 ONU and OLT from Baudcom Jenny training PPT 3分钟读懂诺里奇艺术大学毕业证NUA毕业证学历认证 想要安全提高成绩?我们的黑客技术采用深度伪装和多层加密手段,确保你的信息安全无忧。价格公道,流程简单,同时提供全面的信息保护和事后痕迹清理,让你轻松提升G... 3分钟读懂圭尔夫大学毕业证U of G毕业证学历认证 Featured (20)
How Race, Age and Gender Shape Attitudes Towards Mental Health AI Trends in Creative Operations 2024 by Artwork Flow.pdf PEPSICO Presentation to CAGNY Conference Feb 2024 Content Methodology: A Best Practices Report (Webinar) How to Prepare For a Successful Job Search for 2024 Social Media Marketing Trends 2024 // The Global Indie Insights Trends In Paid Search: Navigating The Digital Landscape In 2024 5 Public speaking tips from TED - Visualized summary ChatGPT and the Future of Work - Clark Boyd Getting into the tech field. what next Google's Just Not That Into You: Understanding Core Updates & Search Intent How to have difficult conversations Introduction to Data Science Time Management & Productivity - Best Practices The six step guide to practical project management Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright... Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present... 12 Ways to Increase Your Influence at Work What is Django model and how to config and migrate
- 1. 4.1 模型基础
模型是项目的数据来源。
每个模型都是一个 Python 类,并且映射到一个数据库表。
模型的每个属性相当于数据库表的一个字段。
使用模型对象可完成各种数据库表操作。
本节主要内容
定义模型
模型配置
迁移数据库
定义字段
- 3. 4.1.2 模型配置
要使用模型,还需要在项目配置文件 settings.py中完成相应的设置。
首先,需要在项目配置文件的 INSTALLED_APPS 变量中添加包含模型的应用名称,
代码如下。
INSTALLED_APPS = [……,
'faqs’, ]
其次,需要在项目配置文件的 DATABASES 变量中设置数据库信息,项目默认的数
据库配置信息如下。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}}
- 4. MySQL 数据库的配置如下。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysqldb',
'USER': 'sqldbuser1',
'PASSWORD': 'sqldbpassword1',
'HOST': '',
'PORT': '',
}
}
- 5. Oracle 数据库的配置如下。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'myordb',
'USER': 'ordbuser1',
'PASSWORD': 'ordbpassword1',
'HOST': '127.0.0.1',
'PORT': '1540',
}
}
- 6. PostgreSQL 数据库的配置如下。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mypsdb',
'USER': 'psdbuser1',
'PASSWORD': 'psdbpassworda1',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
- 7. 4.1.3 迁移数据库
完成模型的定义和配置后,在使用数据库之前,还需执行数据库迁
移操作。
Django 通过迁移操作将模型的更改(模型定义、模型删除、字段更
改等)应用到数据库。
首先,执行 makemigrations 命令,根据模型的更改情况生成迁移
文件。
E:chapter4>python manage.py makemigrations
生成迁移文件后,执行 migrate 命令,应用迁移文件完成迁移操作
E:chapter4>python manage.py migrate
- 8. 4.1.4 定义字段
字段的定义包括字段名、字段类型和字段选项
示例代码如下。
question=models.CharField(max_length=200,blank=True)
其中, question 为字段名, CharField 为字段类型, max_length 和
blank 为字段选项。
Django 在 django.db.models.fields 模块中定义了可用的字段类型。
为了方便, Django 已将 fields 模块导入到 django.db.models 模块中。
通常,先用 from django.db import models 导入 models 模块,然
后用 models.xxxField 来引用字段类型。
- 9. 表 4-1 字段类型
字段类型 说明
AutoField 自动增量, 32 位整数,取值范围是 1~231
-1
BigAutoField 自动增量, 64 位整数,取值范围是 1~263
-1
BigIntegerField 64 位整数,取值范围是 -263
~263
-1 。字段的默认表单控件为 TextInput
BinaryField 存储原始二进制数据
BooleanField 存储 True 或 False 。字段的默认表单控件为 CheckboxInput
CharField 存储字符串。字段的默认表单控件为 TextInput
DateField 存储日期,字段值为 datetime.date 实例。字段的默认表单控件为 TextInput
DateTimeField
存储日期时间,字段值为 datetime.datetime 实例。字段的默认表单控件为
TextInput
DecimalField
存储固定精度的十进制数字段,字段值为 Decimal 实例。字段的默认表单控件
为 NumberInput
DurationField 存储时间段
EmailField 存储 E-mail 地址
FileField 存储文件。字段的默认表单控件为 ClearableFileInput
FilePathField 存储文件路径
FloatField 存储浮点数字。字段的默认表单控件为 NumberInput
- 10. 表 4-1 字段类型(续)
字段类型 说明
IntegerField 存储整数。取值范围是 -231
~231
-1 。字段的默认表单控件为 NumberInput
GenericIPAddressField 存储字符串格式的 IPv4 或 IPv6 地址。字段的默认表单控件为 TextInput
PositiveIntegerField 存储非负整数。取值范围是 0~231
-1
PositiveSmallIntegerField 存储非负小整数。取值范围是 0~215
-1
SlugField 存储 Slug 数据,只包含字母、数字、下划线或连字符
SmallIntegerField 存储小整数。取值范围是 -215
~215
-1
TextField 存储大量文本。字段的默认表单控件为 Textarea
TimeField 存储时间,字段值为 datetime.time 实例。字段的默认表单控件为 TextInput
URLField 存储 URL 。字段的默认表单控件为 TextInput
UUIDField 存储唯一标识符,字段值为 UUID 类实例
- 11. 表 4-2 字段选项
选项 说明
null
默认为 False 。为 True 时, Django 在字段无数据时将空值 NULL 存入数据库(字符串字段存入空字符
串)
blank
默认为 False 。为 True 时,字段允许为空,即表单验证将允许输入空值。 blank 影响数据验证, null
影响数据库数据存储
choices 为字段定义选择项。字段值为选择项中的列表或元组中的值
db_column 定义字段在数据库表中的列名称。未设置时, Django 用模型中的字段名作为数据库表列名称
db_index 为 True 时,为该字段创建数据库索引
db_tablespace 若为字段创建了索引,则为字段索引设置数据库的表空间名称
default 设置字段默认值
editable 默认是 True 。为 False 时,字段不在模型表单中显示
error_messages 设置错误提示信息。该设置会覆盖默认的错误提示信息
help_text 设置字段的帮助信息
primary_key 设置为 True 时,字段成为模型的主键。
unique 设置为 True 时,字段值在整个表中必须是唯一的
unique_for_date 设置为日期或日期时间字段名,关联的两个字段值在整个表中必须是唯一的
unique_for_month 类似 unique_for_date 。与关联的月份唯一
unique_for_year 类似 unique_for_date 。与关联的年份唯一
verbose_name 为字段设置备注名称