248 第六篇:Django-模型层-单表操作

一 ORM简介

查询数据层次图解:如果操作mysql,ORM是在pymysq之上又进行了一层封装

 

  • MVC或者MTV框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的工作量,不需要面对因数据库变更而导致的无效劳动
  • ORM是“对象-关系-映射”的简称。

 

#sql中的表                                                      

#创建表:
CREATE TABLE employee(
id INT PRIMARY KEY auto_increment ,
name VARCHAR (
20),
gender BIT default
1,
birthday DATA ,
department VARCHAR (
20),
salary DECIMAL (
8,2) unsigned,
);

#sql中的表纪录

#添加一条表纪录:
INSERT employee (name,gender,birthday,salary,department)
VALUES (
"alex",1,"1985-12-12",8000,"保洁部");

#查询一条表纪录:
SELECT * FROM employee WHERE age=24;

#更新一条表纪录:
UPDATE employee SET birthday="1989-10-24" WHERE id=1;

#删除一条表纪录:
DELETE FROM employee WHERE name="alex"

#python的类
class Employee(models.Model):
id
=models.AutoField(primary_key=True)
name
=models.CharField(max_length=32)
gender
=models.BooleanField()
birthday
=models.DateField()
department
=models.CharField(max_length=32)
salary
=models.DecimalField(max_digits=8,decimal_places=2)

#python的类对象
#添加一条表纪录:
emp=Employee(name="alex",gender=True,birthday="1985-12-12",epartment="保洁部")
emp.save()
#查询一条表纪录:
Employee.objects.filter(age=24)
#更新一条表纪录:
Employee.objects.filter(id=1).update(birthday="1989-10-24")
#删除一条表纪录:
Employee.objects.filter(name="alex").delete()

示例

二 单表操作

创建表

1 创建模型

创建名为book的app,在book下的models.py中创建模型:

复制代码
from django.db import models

# Create your models here.
class Book(models.Model):
id
= models.AutoField(primary_key=True)
name
= models.CharField(max_length=64)
pub_data
= models.DateField()
price
= models.DecimalField(max_digits=5, decimal_places=2)
publish
= models.CharField(max_length=12)
def str(self):
return self.name

复制代码

2 更多字段和参数

每个字段有一些特有的参数,例如,CharField需要max_length参数来指定VARCHAR数据库字段的大小。还有一些适用于所有字段的通用参数。 这些参数在文档中有详细定义,这里我们只简单介绍一些最常用的:

AutoField(Field)
        - int自增列,必须填入参数 primary_key=True
BigAutoField(AutoField)
    </span>- bigint自增列,必须填入参数 primary_key=<span style="color: rgba(0, 0, 0, 1)">True

    注:当model中如果没有自增列,则自动会创建一个列名为id的列
    </span><span style="color: rgba(0, 0, 255, 1)">from</span> django.db <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> models

    </span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> UserInfo(models.Model):
        </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 自动创建一个列名为id的且为自增的整数列</span>
        username = models.CharField(max_length=32<span style="color: rgba(0, 0, 0, 1)">)

    </span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Group(models.Model):
        </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 自定义自增列</span>
        nid = models.AutoField(primary_key=<span style="color: rgba(0, 0, 0, 1)">True)
        name </span>= models.CharField(max_length=32<span style="color: rgba(0, 0, 0, 1)">)

SmallIntegerField(IntegerField):
    </span>- 小整数 -32768 ~ 32767<span style="color: rgba(0, 0, 0, 1)">

PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField)
    </span>- 正小整数 0 ~ 32767<span style="color: rgba(0, 0, 0, 1)">
IntegerField(Field)
    </span>- 整数列(有符号的) -2147483648 ~ 2147483647<span style="color: rgba(0, 0, 0, 1)">

PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField)
    </span>- 正整数 0 ~ 2147483647<span style="color: rgba(0, 0, 0, 1)">

BigIntegerField(IntegerField):
    </span>- 长整型(有符号的) -9223372036854775808 ~ 9223372036854775807<span style="color: rgba(0, 0, 0, 1)">

自定义无符号整数字段

    </span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> UnsignedIntegerField(models.IntegerField):
        </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> db_type(self, connection):
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">integer UNSIGNED</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">

    PS: 返回值为字段在数据库中的属性,Django字段默认的值为:
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">AutoField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">integer AUTO_INCREMENT</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">BigAutoField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bigint AUTO_INCREMENT</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">BinaryField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">longblob</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">BooleanField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bool</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">CharField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">varchar(%(max_length)s)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">CommaSeparatedIntegerField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">varchar(%(max_length)s)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DateField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">date</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DateTimeField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">datetime</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DecimalField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">numeric(%(max_digits)s, %(decimal_places)s)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DurationField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bigint</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FileField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">varchar(%(max_length)s)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FilePathField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">varchar(%(max_length)s)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FloatField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">double precision</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">IntegerField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">integer</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">BigIntegerField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bigint</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">IPAddressField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">char(15)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">GenericIPAddressField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">char(39)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">NullBooleanField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bool</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">OneToOneField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">integer</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">PositiveIntegerField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">integer UNSIGNED</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">PositiveSmallIntegerField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">smallint UNSIGNED</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">SlugField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">varchar(%(max_length)s)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">SmallIntegerField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">smallint</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">TextField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">longtext</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">TimeField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">time</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">UUIDField</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">char(32)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,

BooleanField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 布尔值类型

NullBooleanField(Field):
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 可以为空的布尔值

CharField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符类型
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 必须提供max_length参数, max_length表示字符长度

TextField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 文本类型

EmailField(CharField):
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串类型,Django Admin以及ModelForm中提供验证机制

IPAddressField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串类型,Django Admin以及ModelForm中提供验证 IPV4 机制

GenericIPAddressField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串类型,Django Admin以及ModelForm中提供验证 Ipv4和Ipv6
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 参数:
        protocol,用于指定Ipv4或Ipv6, </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">both</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ipv4</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ipv6</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
        unpack_ipv4, 如果指定为True,则输入::ffff:</span>192.0.2.1时候,可解析为192.0.2.1,开启刺功能,需要protocol=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">both</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">

URLField(CharField)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串类型,Django Admin以及ModelForm中提供验证 URL

SlugField(CharField)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串类型,Django Admin以及ModelForm中提供验证支持 字母、数字、下划线、连接符(减号)

CommaSeparatedIntegerField(CharField)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串类型,格式必须为逗号分割的数字

UUIDField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串类型,Django Admin以及ModelForm中提供对UUID格式的验证

FilePathField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串,Django Admin以及ModelForm中提供读取文件夹下文件的功能
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 参数:
            path,                      文件夹路径
            match</span>=<span style="color: rgba(0, 0, 0, 1)">None,                正则匹配
            recursive</span>=<span style="color: rgba(0, 0, 0, 1)">False,           递归下面的文件夹
            allow_files</span>=<span style="color: rgba(0, 0, 0, 1)">True,          允许文件
            allow_folders</span>=<span style="color: rgba(0, 0, 0, 1)">False,       允许文件夹

FileField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串,路径保存在数据库,文件上传到指定目录
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 参数:
        upload_to </span>= <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">      上传文件的保存路径
        storage </span>=<span style="color: rgba(0, 0, 0, 1)"> None      存储组件,默认django.core.files.storage.FileSystemStorage

ImageField(FileField)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 字符串,路径保存在数据库,文件上传到指定目录
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 参数:
        upload_to </span>= <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">      上传文件的保存路径
        storage </span>=<span style="color: rgba(0, 0, 0, 1)"> None      存储组件,默认django.core.files.storage.FileSystemStorage
        width_field</span>=<span style="color: rgba(0, 0, 0, 1)">None,   上传图片的高度保存的数据库字段名(字符串)
        height_field</span>=<span style="color: rgba(0, 0, 0, 1)">None   上传图片的宽度保存的数据库字段名(字符串)

DateTimeField(DateField)
    </span>- 日期+时间格式 YYYY-MM-<span style="color: rgba(0, 0, 0, 1)">DD HH:MM[:ss[.uuuuuu]][TZ]

DateField(DateTimeCheckMixin, Field)
    </span>- 日期格式      YYYY-MM-<span style="color: rgba(0, 0, 0, 1)">DD

TimeField(DateTimeCheckMixin, Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 时间格式      HH:MM[:ss[.uuuuuu]]

DurationField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 长整数,时间间隔,数据库中按照bigint存储,ORM中获取的值为datetime.timedelta类型

FloatField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 浮点型

DecimalField(Field)
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 10进制小数
    </span>-<span style="color: rgba(0, 0, 0, 1)"> 参数:
        max_digits,小数总长度
        decimal_places,小数位长度

BinaryField(Field)
    </span>- 二进制类型</pre>
字段
(1)null

如果为True,Django 将用NULL 来在数据库中存储空值。 默认值是 False.

(1)blank

如果为True,该字段允许不填。默认为False。
要注意,这与 null 不同。null纯粹是数据库范畴的,而 blank 是数据验证范畴的。
如果一个字段的blank=True,表单的验证将允许该字段是空值。如果字段的blank=False,该字段就是必填的。

(2)default

字段的默认值。可以是一个值或者可调用对象。如果可调用 ,每有新对象被创建它都会被调用。

(3)primary_key

如果为True,那么这个字段就是模型的主键。如果你没有指定任何一个字段的primary_key=True,
Django 就会自动添加一个IntegerField字段做为主键,所以除非你想覆盖默认的主键行为,
否则没必要设置任何一个字段的primary_key
=True。

(4)unique

如果该值设置为 True, 这个数据字段的值在整张表中必须是唯一的

(5)choices
由二元组组成的一个可迭代对象(例如,列表或元组),用来给字段提供选择项。 如果设置了choices ,默认的表单将是一个选择框而不是标准的文本框,
<br>而且这个选择框的选项就是choices 中的选项。

参数
 class UserInfo(models.Model):
        nid = models.AutoField(primary_key=True)
        username = models.CharField(max_length=32)
        class Meta:
            # 数据库中生成的表名称 默认 app名称 + 下划线 + 类名
            db_table = "table_name"
        <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 联合索引</span>
        index_together =<span style="color: rgba(0, 0, 0, 1)"> [
            (</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">pub_date</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">deadline</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">),
        ]

        </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 联合唯一索引</span>
        unique_together = ((<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">driver</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">restaurant</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">),)

        </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> admin中显示的表名称</span>

verbose_name

        </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> verbose_name加s</span>
        verbose_name_plural</pre>
元信息

3 settings配置

若想将模型转为mysql数据库中的表,需要在settings中配置:

复制代码

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'lqz',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': 3306,
'ATOMIC_REQUEST': True,
'OPTIONS': {
"init_command": "SET storage_engine=MyISAM",
}
}
}
'''
'NAME':要连接的数据库,连接前需要创建好
'USER':连接数据库的用户名
'PASSWORD':连接数据库的密码
'HOST':连接主机,默认本机
'PORT':端口 默认3306
'ATOMIC_REQUEST': True,
设置为True统一个http请求对应的所有sql都放在一个事务中执行(要么所有都成功,要么所有都失败)。
是全局性的配置, 如果要对某个http请求放水(然后自定义事务),可以用non_atomic_requests修饰器
'OPTIONS': {
"init_command": "SET storage_engine=MyISAM",
}
设置创建表的存储引擎为MyISAMINNODB
'''
 
复制代码

注意1:NAME即数据库的名字,在mysql连接前该数据库必须已经创建,而上面的sqlite数据库下的db.sqlite3则是项目自动创建 USER和PASSWORD分别是数据库的用户名和密码。设置完后,再启动我们的Django项目前,我们需要激活我们的mysql。然后,启动项目,会报错:no module named MySQLdb 。这是因为django默认你导入的驱动是MySQLdb,可是MySQLdb 对于py3有很大问题,所以我们需要的驱动是PyMySQL 所以,我们只需要找到项目名文件下的__init__,在里面写入:

import pymysql
pymysql.install_as_MySQLdb()

最后通过两条数据库迁移命令即可在指定的数据库中创建表 :

python manage.py makemigrations
python manage.py migrate

注意2:确保配置文件中的INSTALLED_APPS中写入我们创建的app名称

复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "book"
]
复制代码

注意3:如果报错如下:

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None

MySQLclient目前只支持到python3.4,因此如果使用的更高版本的python,需要修改如下:

通过查找路径C:\Programs\Python\Python36-32\Lib\site-packages\Django-2.0-py3.6.egg\django\db\backends\mysql
这个路径里的文件把

if version < (1, 3, 3):
     raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

注释掉就可以了

注意4: 如果想打印orm转换过程中的sql,需要在settings中进行如下配置:

复制代码
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level':'DEBUG',
        },
    }
}
复制代码

4 增加,删除字段

  删除,直接注释掉字段,执行数据库迁移命令即可

  新增字段,在类里直接新增字段,直接执行数据库迁移命令会提示输入默认值,此时需要设置

publish = models.CharField(max_length=12,default='人民出版社',null=True)

 

注意:

  1 数据库迁移记录都在 app01下的migrations里

  2 使用showmigrations命令可以查看没有执行migrate的文件

  3  makemigrations是生成一个文件,migrate是将更改提交到数据量

添加表纪录

方式1 

# create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象
book_obj=Book.objects.create(title="python葵花宝典",state=True,price=100,publish="苹果出版社",pub_date="2012-12-12")

方式2

book_obj=Book(title="python葵花宝典",state=True,price=100,publish="苹果出版社",pub_date="2012-12-12")
book_obj.save()

查询表纪录

查询API

复制代码
<1> all():                  查询所有结果

<2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象

<3> get(**kwargs): 返回与所给筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或者没有都会抛出错误。

<4> exclude(**kwargs): 它包含了与所给筛选条件不匹配的对象

<5> order_by(*field): 对查询结果排序('-id')

<6> reverse(): 对查询结果反向排序

<8> count(): 返回数据库中匹配查询(QuerySet)的对象数量。

<9> first(): 返回第一条记录

<10> last(): 返回最后一条记录

<11> exists(): 如果QuerySet包含数据,就返回True,否则返回False

<12> values(field): 返回一个ValueQuerySet——一个特殊的QuerySet,运行后得到的并不是一系列
model的实例化对象,而是一个可迭代的字典序列
<13> values_list(
field): 它与values()非常相似,它返回的是一个元组序列,values返回的是一个字典序列

<14> distinct(): 从返回结果中剔除重复纪录

复制代码

 

def index(request):
    # 添加表记录++++++++++++++++++++++++++++++++++
    # 方式一
    # book=Book(name='红楼梦',pub_data='2015-10-12',price=88,publish='老男孩出版社')
    # book.save()
    # 方式二
    # Book.objects.create(name='Python红宝书',pub_data='2010-10-12',price=100,publish='人民出版社')
    # 查询表记录++++++++++++++++++++++++++++++++++
    # QUerySet数据类型(类似于一个列表,里面放着一些对象)
    # 1 方法的返回值是什么
    # 2 方法的调用者
    # (1) all方法 返回一个QuerySet对象
    # book_list=Book.objects.all()
    # print(book_list[1].name)
    # print(book_list)
    # for obj in book_list:
    #     print(obj.name)
    # (2)first last:调用者是queryset对象,返回值是对象
    # book=Book.objects.all().first()
    # book2=Book.objects.all().last()
    # print(book)
    # print(book2)
    # (3) filter  返回值是queryset对象(相当于where语句)
    # 可以加多个过滤条件
    # book=Book.objects.filter(name='红楼梦').first()
    # print(book)
    # (4)get方法 有且只有一个查询结果才有意义 返回值是一个对象
    # book=Book.objects.get(name='红楼梦')
    # print(book)
    # 直接报错
    # book = Book.objects.get(name='红楼梦eee')
    # --------------最常用-----------------
    # (5)exclude 除了查询之外的 返回值也是queryset
    # ret=Book.objects.exclude(name='红楼梦')
    # print(ret)
    # (6)order_by(默认升序,加个- 就是降序),可以多个过滤条件调用者是queryset返回值也是queryset
    # book_list=Book.objects.all().order_by('id')
    # book_list=Book.objects.all().order_by('-id','price')
    # print(book_list)
    # (7)count() 调用者是queryset,返回值是int
    # ret=Book.objects.all().count()
    # print(ret)
    # (8)exist()判断是是否有值,不能传参数,
    # ret=Book.objects.all().exists()
    # print(ret)
    # (9)values方法
    # 查询所有书籍的名称(里面传的值,前提是表有这个字段)也是queryset但是里面放的是字典
    '''
    values原理
    temp=[]
    for obj in Book.objects.all():
        temp.append({'name':obj.name})
    '''
    # ret=Book.objects.all().values('name')
    # print(ret)
    # 不加.all()也可以,调用是queryset返回值也是queryset
    # ret=Book.objects.values('price')
    # print(ret)
    # (10)value_list
    # ret=Book.objects.all().values_list('price','name')
    # print(ret)
    # (11) distinct  seletc * 的时候没有意义
    # SELECT DISTINCT name from app01_book;
    # 没有任何意义,不要这样么用
    # Book.objects.all().distinct()
    # ret=Book.objects.all().values('name').distinct()
    # print(ret)
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 双下划线模糊查询-----------------------</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 查询价格大于100的书</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(price__gt=100)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 查询大于50小于100的书</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(price__gt=50,price__lt=100)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 查询已红楼开头的书</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(name__startswith='红楼')</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 查询包含‘红’的书</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret= Book.objects.filter(name__contains='红')</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> icontains  不区分大小写</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 价格在50,88,100 中的</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(price__in=[50,88,100])</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 出版日期在2018年的</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(pub_data__year=2015,pub_data__month=2)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 删除,修改------------------------</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> delete:调用者可以是queryset也可以是model对象</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 删除价格为188的书有返回值 (1, {'app01.Book': 1}) 删除的个数,那张表,记录数</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(price=188).delete()</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(price=100).first().delete()</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>

<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 修改 update只能queryset来调用 返回值为int</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(name='红楼梦1').update(name='红楼梦')</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 报错</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Book.objects.filter(name='红楼梦').first().update(name='红楼梦1')</span>

<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ret=Book.objects.filter(name='红楼梦1').first()</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(ret.delete())</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> aa=Publish.objects.filter(name='人民出版社')</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(type(aa))</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> aa.delete()</span>

<span style="color: rgba(0, 0, 255, 1)">return</span> HttpResponse(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ok</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</pre>
测试数据

 

基于双下划线的模糊查询 

复制代码
Book.objects.filter(price__in=[100,200,300])
Book.objects.filter(price__gt=100)
Book.objects.filter(price__lt=100)
Book.objects.filter(price__gte=100)
Book.objects.filter(price__lte=100)
Book.objects.filter(price__range=[100,200])
Book.objects.filter(title__contains="python")
Book.objects.filter(title__icontains="python")
Book.objects.filter(title__startswith="py")
Book.objects.filter(pub_date__year=2012)
复制代码

 

删除表纪录

删除方法就是 delete()。它运行时立即删除对象而不返回任何值。例如:

model_obj.delete()

你也可以一次性删除多个对象。每个 QuerySet 都有一个 delete() 方法,它一次性删除 QuerySet 中所有的对象。

例如,下面的代码将删除 pub_date 是2005年的 Entry 对象:

Entry.objects.filter(pub_date__year=2005).delete()

在 Django 删除对象时,会模仿 SQL 约束 ON DELETE CASCADE 的行为,换句话说,删除一个对象时也会删除与它相关联的外键对象。例如:

b = Blog.objects.get(pk=1)
# This will delete the Blog and all of its Entry objects.
b.delete()

 

要注意的是: delete() 方法是 QuerySet 上的方法,但并不适用于 Manager 本身。这是一种保护机制,是为了避免意外地调用 Entry.objects.delete() 方法导致 所有的 记录被误删除。如果你确认要删除所有的对象,那么你必须显式地调用:

Entry.objects.all().delete() 

如果不想级联删除,可以设置为:

pubHouse = models.ForeignKey(to='Publisher', on_delete=models.SET_NULL, blank=True, null=True)

修改表纪录

Book.objects.filter(title__startswith="py").update(price=120

此外,update()方法对于任何结果集(QuerySet)均有效,这意味着你可以同时更新多条记录update()方法会返回一个整型数值,表示受影响的记录条数。

三 在Python脚本中调用Django环境

复制代码
import os
if __name__ == '__main__':
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "untitled15.settings")
    import django
    django.setup()
</span><span style="color: rgba(0, 0, 255, 1)">from</span> app01 <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> models

books </span>=<span style="color: rgba(0, 0, 0, 1)"> models.Book.objects.all()
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(books)</pre>
复制代码

四 Django终端打印SQL语句

复制代码
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level':'DEBUG',
        },
    }
}
复制代码

 章节作业

1 图书管理系统

实现功能:book单表的增删改查

2 查询操作练习

复制代码
1 查询老男孩出版社出版过的价格大于200的书籍

2 查询2017年8月出版的所有以py开头的书籍名称

3 查询价格为50,100或者150的所有书籍名称及其出版社名称

4 查询价格在100到200之间的所有书籍名称及其价格

5 查询所有人民出版社出版的书籍的价格(从高到低排序,去重)

复制代码

 

posted @ 2020-01-02 20:19  ABDM  阅读(361)  评论(0编辑  收藏  举报