【Django】ORM与Model

2022/2/2 23:42:58

本文主要是介绍【Django】ORM与Model,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

什么是ORM

Object Relational Mapping,即对象关系映射

使得Python编程人员不用直接编写SQL代码,可以像操作对象一样仅用Python语言操作数据库中的数据

Model

模型用于描述数据,它包含了储存的数据的重要字段行为

  • 每个模型都是一个 Python 的类,这些类继承了 django.db.models.Model
  • 每一个模型都映射一张数据库表
  • 模型类的每个属性都相当于一个数据库的字段

常用类型及属性介绍

函数名

类型

示例

通用属性

models.IntegerField()

整型

(-2147483648,2147483647)

  • primary_key

    • 设置主键

    • True

  • null

    • 值是否为null

    • True/False

  • blank

    • 值是否为空

    • True/False

  • default

    • 默认值

  • verbose_name

    • admin中显示的名字

  • db_column

    • 数据库字段名

  • unique

    • 唯一索引

    • True

  • db_index

    • 普通索引

    • True

models.SmallIntegerField()

短整型

(-32768,32767)

models.BigIntegerField()

长整型

models.PositiveIntegerField()

正整型

(0,2147483647)

models.PositiveSmallIntegerField()

短正整型

(0,32768)

models.CharField()

字符串

'aqin'

  • max_length
    • 字段长度

models.FloatField()

浮点型

8.88

models.DecimalField()

十进制小数

8.88888

  • max_digits

    • 数字中允许的最大位数

  • decimal_places

    • 存储的十进制位数

models.BooleanField()

布尔型

True/False

models.NullBooleanField()

可为空布尔型

True/False/None

models.TextField()

文本

'hello aqin'

models.EmailField()

邮箱

'12345678@qq.com'

models.UrlField()

网址

'http://www.xxx.com'

models.DateField()

日期

(年-月-日 )

2022-02-02

  • auto_now

    • 时间自动添加

    • True

  • auto_now_add

    • 时间自动添加(仅在创建的时候添加一次)

    • True

models.DateTimeField()

日期

(年-月-日 时:分:秒)

2022-02-02 12:12:12

models.TimeField()

日期

(时:分:秒)

12:12:12

models.ImageField()

图片

  • width_field

    • 图片宽

  • height_field

    • 图片高

  • upload_to

    • 上传图片的本地路径

models.FileField()

文件

任意文件类型

  • upload_to

    • 上传文件的本地路径

实践

1. 创建数据库模型

models.py中的代码:

from django.db import models

# 基于类的数据库模型

# 继承内置的ORM(models.Model)

class User(models.Model):

id = models.IntegerField(primary_key=True)

name = models.CharField(max_length=20, unique=True, blank=False)

age = models.IntegerField(default=0)

phone_number = models.EmailField(blank=True, default='')

# 创建时添加

created_time = models.DateTimeField(auto_now_add=True)

# 更新时变更时间

modified_time = models.DateTimeField(auto_now=True)

2. 创建迁移脚本文件

查找所有可用的模型,为任意一个在数据库中不存在对应数据表的模型创建迁移脚本文件

python manage.py makemigrations
(tutorial-env) aqin1012@aqin1012deMBP mysite % python manage.py makemigrations

Migrations for 'app':

app/migrations/0001_initial.py

- Create model User

3. 创建数据库表

运行上一步python manage.py makemigrations生成的迁移脚本来自动创建数据库表

python manage.py migrate
(tutorial-env) aqin1012@aqin1012deMBP mysite % python manage.py migrate

Operations to perform:

Apply all migrations: admin, app, auth, contenttypes, sessions

Running migrations:

Applying contenttypes.0001_initial... OK

Applying auth.0001_initial... OK

Applying admin.0001_initial... OK

Applying admin.0002_logentry_remove_auto_add... OK

Applying admin.0003_logentry_add_action_flag_choices... OK

Applying app.0001_initial... OK

Applying contenttypes.0002_remove_content_type_name... OK

Applying auth.0002_alter_permission_name_max_length... OK

Applying auth.0003_alter_user_email_max_length... OK

Applying auth.0004_alter_user_username_opts... OK

Applying auth.0005_alter_user_last_login_null... OK

Applying auth.0006_require_contenttypes_0002... OK

Applying auth.0007_alter_validators_add_error_messages... OK

Applying auth.0008_alter_user_username_max_length... OK

Applying auth.0009_alter_user_last_name_max_length... OK

Applying auth.0010_alter_group_name_max_length... OK

Applying auth.0011_update_proxy_permissions... OK

Applying auth.0012_alter_user_first_name_max_length... OK

Applying sessions.0001_initial... OK

完成撒花



这篇关于【Django】ORM与Model的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程