慕课网-Django入门到进阶-更适合Python小白的系统课程-第4章Django中的ORM和数据库-4-2列方法于属性介绍
第4章 Django 中的 ORM 和数据库
4-2 列方法于属性介绍
字段方法所在位置
from django.db import models models.CharField... dir(models)
字符串与数字类型
| 字段名 | 描述 | 例子 |
| CharField | 字符串类型 | 'namexxxx' |
| TextField | 文本类型 | 'xxxxxxxxxxxxxxxxx...' |
| EmailField | 邮箱类型 | 'xxx@muke.com' |
| UrlField | 网址类型 | 'http://muke.com' |
| BooleanField | 布尔类型(tinyint) | True False |
| NullBooleanField | 可为空的布尔类型 | None True False |
| IntegerField | 整型 | (-2147483648, 2147483647) |
| SmallIntegerField | 短整型 | (-32768, 32767) |
| BigIntegerField | 长整型 | (-9223372036854775808, 9223372036854775807) |
| PositiveIntegerField | 正整型 | (0, 2147483647) |
| PositiveSmallIntegerField | 短正整型 | (0, 32767) |
| FloatField | 浮点类型 | 3.14 |
| DecimalField | 十进制小数 | 12345.123123 |
时间类型
| 字段名 | 描述 | 例子 |
| DateField | 日期类型 | xxxx-xx-xx |
| DateTimeField | 日期类型 | xxxx-xx-xx xx:xx:xx |
| TimeField | 时间类型 | xx:xx:xx (时分秒) |
文件类型
| 字段名 | 描述 | 例子 |
| ImageField | 图片类型 | xxx.jpg |
| FileField | 文件类型 | 任意文件类型 |
特殊类型属性介绍
| 属性名 | 描述 | 例子 | 作用于 |
| max_digits | 数字中允许的最大位数 | 12 | DecimalField |
| decimal_places | 存储的十进制位数 | 2 | DecimalField |
| width-field | 图片宽(可不传) | 1024 | ImageField |
| height-field | 图片高(可不传) | 576 | ImageField |
| upload_to | 保存上传文件的本地文件路径,该路径由 MEDIA_ROOT 中设置 | '/xx/xx.xx' | ImageField, FileField |
公共属性介绍
| 属性名 | 描述 | 例子 | 作用于 |
| null | 值是否设为空 | True False | |
| blank | 值是否可为空 | True False | |
| primary_key | 设置主键 | True | 整型 |
| auto_now | 时间自动添加 | True | 时间类型 |
| auto_now_add | 自动添加时间,但仅在创建的时候 | True | 时间类型 |
| max_length | 字段长度 | 字符串类型 | |
| default | 默认值 | xxx | |
| verbose_name | admin 中显示的名字 | name | |
| db_column | 数据库字段名 | age | |
| unique | 唯一索引 | True | |
| db_index | 普通索引 | True |
表关联方法
| 字段名 | 描述 | 例子 |
| ForeignKey | 一对多 | |
| OneToOneField | 一对一 | |
| ManyToManyField | 多对多 |
| 属性名 | 描述 | 例子 |
| related_name | 关联表的名 | related_name='profile' |
| on_delete | 外键的删除的对策 | on_delete=models.SET_NULL(CASCADE, PROTECT) |
实例
1.在项目 four 下目录 app,修改文件 models.py,添加数据库表 User
#coding: utf-8
from django.db import models
class Test(models.Model):
name = models.IntegerField()
class User(models.Model):
id = models.IntegerField(primary_key=True)
username = models.CharField(unique=True, max_length=50, blank=False)
age = models.SmallIntegerField(default=0)
phone = models.SmallIntegerField(db_index=True, blank=True, default=0)
email = models.EmailField(blank=True, default='')
info = models.TextField()
create_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
2.命令窗口创建数据库表 User
python manage.py makemigrations python manage.py migrate

3.进入数据库测试
use lession4 show tables; show create table app_user;

4.在项目 four 下目录 app,修改文件 models.py,修改 phone 字段长度为 IntegerField
#coding: utf-8
from django.db import models
class Test(models.Model):
name = models.IntegerField()
class User(models.Model):
id = models.IntegerField(primary_key=True)
username = models.CharField(unique=True, max_length=50, blank=False)
age = models.SmallIntegerField(default=0)
phone = models.IntegerField(db_index=True, blank=True, default=0)
email = models.EmailField(blank=True, default='')
info = models.TextField()
create_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
5.命令窗口修改数据库表 User
python manage.py makemigrations python manage.py migrate

6.进入数据库测试
use lession4 show tables; show create table app_user;

posted on 2020-01-28 10:56 herisson_pan 阅读(14) 评论(0) 收藏 举报
浙公网安备 33010602011771号