Django学习系列14:第一个数据库迁移
在Django中,ORM的任务是模型化数据库。
创建数据库其实是由另一个系统负责的叫做迁移。
迁移的任务是根据你对models.py文件的改动情况,添加或删除表和列。
可以把迁移想象成数据库使用的版本控制系统,后面会看到,把应用部署到线上服务器审计数据库时,迁移十分有用。
现在只需要知道如何创建第一个数据库迁移,使用makemigrations命令创建迁移:
# python manage.py makemigrations
Migrations for 'lists':
lists/migrations/0001_initial.py
- Create model Item
# ls lists/migrations/
0001_initial.py __init__.py __pycache__
Python一开始能给.text属性赋值,继承models.Model的类映射到数据库中的一个表,默认情况下,这种类会得到一个自动生成的id属性,作为表的主键,但是其他列都要自行定义。
# lists/models.py from django.db import models # Create your models here.
from django.db import models class Item(models.Model):
text = models.TextField()
django提供了很多其他字段类型,IntegerField, CharField, DateField等,使用TextField 而不是CharField,是因为CharField需要限制长度,目前,这个字段的长度是随意的。关于字段类型的更多介绍可以阅读djang教程和文档
添加新字段就要创建新迁移
运行测试得到另一个数据库错误# python manage.py test lists
django.db.utils.OperationalError: no such column: lists_item.text
出现这个错误的原因是在数据库中添加了一个新字段,所以要再创建一个迁移。
创建迁移试试
# python manage.py makemigrations
You are trying to add a non-nullable field 'text' to item without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
) Provide a one-off default now (will be set on all existing rows with a null value for this column)
) Quit, and let me add a default in models.py
Select an option:
这个命令不允许添加没有默认值的列,选择第二个选项,然后在models.py中设定一个默认值。
class Item(models.Model):
text = models.TextField(default='')
现在应该可以顺利创建迁移了。
# python manage.py makemigrations
Migrations for 'lists':
lists/migrations/0002_item_text.py
- Add field text to item
在models.py中添加了两行新代码,创建了两个数据库迁移,由此得到的结果是,模型对象上的.text属性能被识别为一个特殊属性了,因此属性的值能保存在数据库中,测试也能通过了
# python manage.py test lists
Creating test database for alias 'default'...
System check identified no issues ( silenced).
....
----------------------------------------------------------------------
Ran tests in .105s OK
Destroying test database for alias 'default'...
下面提交创建的第一个模型
$ git status # see tests.py, models.py, and untracked migrations(看见tests.py, models.py以及两个没有跟踪的迁移文件)
$ git diff # review changes to tests.py and models.py(审查tests.py and models.py)
$ git add lists
$ git commit -m "Model for list Items and associated migration——列表项和相关迁移的模型 "
Django学习系列14:第一个数据库迁移的更多相关文章
- Django学习系列之Form基础
Django学习系列之Form基础 2015-05-15 07:14:57 标签:form django 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追 ...
- Python学习系列(七)( 数据库编程)
Python学习系列(七)( 数据库编程) Python学习系列(六)(模块) 一,MySQL-Python插件 Python里操作MySQL数据库,需要Python下安装访 ...
- [更新中]【South使用总结】django开发中使用South进行数据库迁移
Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...
- Django 学习笔记(七)数据库基本操作(增查改删)
一.前期准备工作,创建数据库以及数据表,详情点击<Django 学习笔记(六)MySQL配置> 1.创建一个项目 2.创建一个应用 3.更改settings.py 4.更改models.p ...
- Entity Framework 5.0系列之Code First数据库迁移
我们知道无论是"Database First"还是"Model First"当模型发生改变了都可以通过Visual Studio设计视图进行更新,那么对于Cod ...
- Django学习day7——简单的使用数据库和模型
Django支持的数据库 PostgreSQL SQLite 3 MySQL Oracle 其中SQLite 3不需要安装,因为SQLite使用文件系统上的独立文件来存储数据 这里我们用SQLite ...
- Django学习系列6:使用selenium测试用户交互
学习系列5中的单元测试有报错信息,这儿来编写functional_tests.py文件,扩充其中的功能测试 # File: functional_test.py # Author: Rxf # Cre ...
- 【Django】依赖auth.user的数据库迁移,以及admin用户非交互式创建
admin用户非交互式创建: echo "from django.contrib.auth.models import User; User.objects.create_superuser ...
- Django学习系列18:使用迁移创建生产数据库
Django生成一个很有帮助的错误信息,大意是说没有正确设置数据库. 你可能会有疑惑,为什么在单元测试一切都运行ok,这是因为Django为单元测试创建了专用的测试数据库——这是Django中Test ...
随机推荐
- 【D3D12学习手记】The Command Queue and Command Lists
GPU有一个命令队列,CPU通过Direct3D API将命令提交到队列里来使用命令列表(command lists),如下图.当一套命令(a set of commands)已经被提交到命令队列,他 ...
- 《Visual Studio程序员箴言》笔记
还记得刚工作时看到某前辈用快捷键操作Visual Studio,赞叹不已,才发觉原来快捷键熟练了效率可以如此之高.后来,我在为一个经常使用的命令添加快捷键的时候又发现Visual Studio的快捷键 ...
- Linux手册页惯用的节名
节 大写 描述 name NAME 显示命令名和一段简短的描述 synopsis SYNOPSIS 命令的语法 confi guration CONFI GURATION 命令配置信息 descrip ...
- android webview 访问 https 页面
在android 中利用webview 控件进行开发过程中,可能会遇到 webview 访问不了https://的页面如 https://www.google.com.hk 重写onReceivedS ...
- OpenStack组件——cinder存储服务
1.cinder 介绍 1)理解 Block Storage 操作系统获得存储空间的方式一般有两种: (1)通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文 ...
- 【Deep Learning Nanodegree Foundation笔记】第 10 课:Sentiment Analysis with Andrew Trask
In this lesson, Andrew Trask, the author of Grokking Deep Learning, will walk you through using neur ...
- mac环境提示:make sure that /usr/local/bin is in your path
今天我在Mac环境下安装了Homebrew和node.js后,输入node -v.brew.npm都提示: command not found,然后我查看了下Homebrew的安装日志,发现日志里有个 ...
- 传统Java Web(非Spring Boot)、非Java语言项目接入Spring Cloud方案--temp
技术架构在向spring Cloud转型时,一定会有一些年代较久远的项目,代码已变成天书,这时就希望能在不大规模重构的前提下将这些传统应用接入到Spring Cloud架构体系中作为一个服务以供其它项 ...
- servlet_filterj简介
Filter总结: 1):Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, ...
- Redis 高可用之哨兵模式(二)
上一篇实际操作过程中遇到两个问题 问题一:虽然运行了3个sentinel容器,实际上只有一个sentinel运行 问题出现的原因很简单,三个sentinel用的是同一个挂载配置文件,容器内部的更改直接 ...