ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Django】ORM与Model

2022-02-02 23:32:29  阅读:172  来源: 互联网

标签:... OK models Django ORM Applying Model True alter


什么是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

完成撒花

标签:...,OK,models,Django,ORM,Applying,Model,True,alter
来源: https://blog.csdn.net/aqin1012/article/details/122772928

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有