一、配置django数据库
默认使用的是sqlite数据库
- 配置sqlite数据库代码为
DATABASES = {
<span class="hljs-string">'default'</span>: {
<span class="hljs-string">'ENGINE'</span>: <span class="hljs-string">'django.db.backends.sqlite3'</span>, <span class="hljs-comment"># 选择数据库引擎</span>
<span class="hljs-string">'NAME'</span>: os.path.join(BASE_DIR, <span class="hljs-string">'db.sqlite3'</span>), <span class="hljs-comment"># 创建sqlite数据库文件位置</span>
}
}
- 配置mysql数据库
DATABASES = {
<span class="hljs-string">'default'</span>: {
<span class="hljs-string">'ENGINE'</span>: <span class="hljs-string">'django.db.backends.mysql'</span>, <span class="hljs-comment"># 选择数据库引擎</span>
<span class="hljs-string">'NAME'</span>: <span class="hljs-string">'lucky'</span>, <span class="hljs-comment">#数据库名称</span>
<span class="hljs-string">'HOST'</span>: <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-comment">#主机</span>
<span class="hljs-string">'USER'</span>: <span class="hljs-string">'lucky'</span>, <span class="hljs-comment">#用户名</span>
<span class="hljs-string">'PASSWORD'</span>: <span class="hljs-string">'123456'</span>, <span class="hljs-comment">#密码</span>
<span class="hljs-string">'PORT'</span>: <span class="hljs-number">3306</span>, <span class="hljs-comment">#端口</span>
}
}
安装pymysql扩展库
代码语言:javascript复制pip3 install pymysql
在project的init.py文件中 添加如下代码
代码语言:javascript复制<span class="hljs-keyword">import</span> pymysql
pymysql.install_as_MySQLdb()
python3.x使用的是pymysql模块,所以需要在project/__init.py文件中添加如上代码
二、表分析
需求: 创建用户表
- 一个用户表所需字段 用户名 密码 性别 年龄 个人简介 头像 是否删除 创建时间
- 创建模型代码位置 App->models.py
- 模型、属性、表之间的关联 一个模型类 对应数据库中的一张表 一个类属性 对应 表中的一个字段
- 创建用户表模型
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(models.Model)</span>:</span>
username = models.CharField(max_length=<span class="hljs-number">20</span>) <span class="hljs-comment"># 用户名 最大长度20</span>
password = models.CharField(max_length=<span class="hljs-number">64</span>) <span class="hljs-comment"># 用户密码 最大长度 64</span>
sex = models.BooleanField(default=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># 性别 默认为True</span>
age = models.IntegerField(default=<span class="hljs-number">20</span>) <span class="hljs-comment"># 年龄 默认20岁</span>
info = models.CharField(max_length=<span class="hljs-number">100</span>,default=<span class="hljs-string">'info'</span>) <span class="hljs-comment"># 个人简介 最大长度100 默认为info</span>
icon = models.CharField(max_length=<span class="hljs-number">60</span>,default=<span class="hljs-string">'default.jpg'</span>) <span class="hljs-comment"># 头像 最大长度为60 默认为 default.jpg</span>
isDelete = models.BooleanField(default=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># 是否删除 默认不删除</span>
createTime = models.DateTimeField(auto_now_add=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># 创建时间 默认为添加数据的时间</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">str</span><span class="hljs-params">(self)</span>:</span> <span class="hljs-comment"># 打印时友好的输出展示</span>
<span class="hljs-keyword">return</span> self.username
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
db_table = <span class="hljs-string">'user'</span> <span class="hljs-comment"># 当前表名(如果不写 默认为应用名称 模型名称)</span>
三、生成数据表
修改DJango原码(注意2.2版本需要此操作)
sudo vim /usr/local/lib/python3.7/dist-packages/django/db/backends/mysql/base.py
代码语言:javascript复制version = Database.version_info
<span class="hljs-keyword">if</span> version < (<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">13</span>):
<span class="hljs-keyword">raise</span> ImproperlyConfigured(<span class="hljs-string">'mysqlclient 1.3.13 or newer is required; you have %s.'</span> % Database.__version__)
更改为:
代码语言:javascript复制version = Database.version_info
<span class="hljs-comment"># if version < (1, 3, 13):</span>
<span class="hljs-comment"># raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)</span>
sudo vim /usr/local/lib/python3.7/dist-packages/django/db/backends/mysql/operations.py
代码语言:javascript复制query = getattr(cursor, <span class="hljs-string">'_executed'</span>, <span class="hljs-keyword">None</span>)
<span class="hljs-keyword">if</span> query <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>:
query = query.decode(errors=<span class="hljs-string">'replace'</span>)
<span class="hljs-keyword">return</span> query
更改为:
代码语言:javascript复制query = getattr(cursor, <span class="hljs-string">'_executed'</span>, <span class="hljs-keyword">None</span>)
<span class="hljs-keyword">if</span> query <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>:
query = query.encode(errors=<span class="hljs-string">'replace'</span>)
<span class="hljs-keyword">return</span> query
生成迁移文件
代码语言:javascript复制python manage.py makemigrations
在’应用目录/migrations’下生成迁移文件 此刻还没有同步到数据库
- 执行迁移
python manage.py migrate
使用迁移文件在数据库中生成对应的数据表 此刻数据库中已经存在我们的用户表
四、测试数据库
- 进入到python shell进行测试 python manage.py shell
- 倒包
<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> App.models <span class="hljs-keyword">import</span> User
- 添加数据
<span class="hljs-meta">>>> </span>u = User() <span class="hljs-comment"># 实例化User模型类</span>
<span class="hljs-meta">>>> </span>u.username = <span class="hljs-string">'lucky'</span> <span class="hljs-comment"># 添加用户名</span>
<span class="hljs-meta">>>> </span>u.password = <span class="hljs-number">123456</span> <span class="hljs-comment"># 添加密码</span>
<span class="hljs-meta">>>> </span>u.sex = <span class="hljs-keyword">True</span> <span class="hljs-comment"># 添加性别</span>
<span class="hljs-meta">>>> </span>u.age = <span class="hljs-number">18</span> <span class="hljs-comment"># 添加年龄</span>
<span class="hljs-meta">>>> </span>u.info = <span class="hljs-string">'lucky是一个帅气的boy'</span> <span class="hljs-comment"># 添加个人简介</span>
<span class="hljs-meta">>>> </span>u.save() <span class="hljs-comment"># 进行保存</span>
- 查询数据
<span class="hljs-meta">>>> </span>u = User.objects.get(pk=<span class="hljs-number">1</span>) <span class="hljs-comment"># 查询主键为1的用户对象</span>
<span class="hljs-meta">>>> </span>u
<span class="hljs-meta">>>> </span>u.username
- 修改
<span class="hljs-meta">>>> </span>u = User.objects.get(pk=<span class="hljs-number">1</span>) <span class="hljs-comment"># 查询主键为1的用户对象</span>
<span class="hljs-meta">>>> </span>u.username = <span class="hljs-string">'帅气的lucky'</span> <span class="hljs-comment"># 修改用户名为帅气的lucky</span>
<span class="hljs-meta">>>> </span>u.save() <span class="hljs-comment"># 进行保存</span>
- 删除
<span class="hljs-meta">>>> </span>u = User.objects.get(pk=<span class="hljs-number">1</span>) <span class="hljs-comment"># 查询主键为1的用户对象</span>
<span class="hljs-meta">>>> </span>u.delete() <span class="hljs-comment"># 进行删除</span>