提炼 http://south.readthedocs.org/en/latest/tutorial/part1.html 官方手册里面的步骤
在新项目中使用
第一步创建项目
django-admin.py startproject LearnSouth
创建app
django-admin.py startapp books
在learnSouth.settings中修改数据库
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'demo.db', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': '', 'PASSWORD': '', 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 'PORT': '', # Set to empty string for default. } } INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'south', 'LearnSouth', 'books', )
命令行执行导入数据库
./manage.py syncdb
初始化迁移信息
./manage.py schemamigration book --initial
执行完以后会在books目录下看到1个migrations文件夹。里面保存了
0001_initial.py 打开看一下。暂时里面没有数据
修改models.py文件内容
from django.db import models # Create your models here. class XiaoShuo(models.Model): create_date = models.DateTimeField(auto_created=True) public_date = models.DateTimeField(auto_now=True) title = models.CharField(max_length=128) contnet = models.CharField(max_length=512000) class Comment(models.Model): username = models.CharField(max_length=64) email = models.CharField(max_length=64) # email = models.EmailField()
执行
./manage.py schemamigration books --auto
查看migrations目录。发现生成
0002_auto_add_xiaoshuo_add_comment.py文件。代码为
class Migration(SchemaMigration): def forwards(self, orm): # Adding model 'XiaoShuo' db.create_table(u'books_xiaoshuo', ( (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('create_date', self.gf('django.db.models.fields.DateTimeField')()), ('public_date', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)), ('title', self.gf('django.db.models.fields.CharField')(max_length=128)), ('contnet', self.gf('django.db.models.fields.CharField')(max_length=512000)), )) db.send_create_signal(u'books', ['XiaoShuo']) # Adding model 'Comment' db.create_table(u'books_comment', ( (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('username', self.gf('django.db.models.fields.CharField')(max_length=64)), ('email', self.gf('django.db.models.fields.CharField')(max_length=64)), )) db.send_create_signal(u'books', ['Comment']) def backwards(self, orm): # Deleting model 'XiaoShuo' db.delete_table(u'books_xiaoshuo') # Deleting model 'Comment' db.delete_table(u'books_comment') models = { u'books.comment': { 'Meta': {'object_name': 'Comment'}, 'email': ('django.db.models.fields.CharField', [], {'max_length': '64'}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'username': ('django.db.models.fields.CharField', [], {'max_length': '64'}) }, u'books.xiaoshuo': { 'Meta': {'object_name': 'XiaoShuo'}, 'contnet': ('django.db.models.fields.CharField', [], {'max_length': '512000'}), 'create_date': ('django.db.models.fields.DateTimeField', [], {}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'public_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'title': ('django.db.models.fields.CharField', [], {'max_length': '128'}) } } complete_apps = ['books']
修改models.py代码为
class XiaoShuo(models.Model): create_date = models.DateTimeField(auto_created=True) public_date = models.DateTimeField(auto_now=True) title = models.CharField(max_length=128) contnet = models.CharField(max_length=512000) class Comment(models.Model): username = models.CharField(max_length=64) email = models.CharField(max_length=64) # email = models.EmailField()
生成0003_auto_chg_field_comment_email.py
import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models class Migration(SchemaMigration): def forwards(self, orm): # Changing field 'Comment.email' db.alter_column(u'books_comment', 'email', self.gf('django.db.models.fields.EmailField')(max_length=75)) def backwards(self, orm): # Changing field 'Comment.email' db.alter_column(u'books_comment', 'email', self.gf('django.db.models.fields.CharField')(max_length=64)) models = { u'books.comment': { 'Meta': {'object_name': 'Comment'}, 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'username': ('django.db.models.fields.CharField', [], {'max_length': '64'}) }, u'books.xiaoshuo': { 'Meta': {'object_name': 'XiaoShuo'}, 'contnet': ('django.db.models.fields.CharField', [], {'max_length': '512000'}), 'create_date': ('django.db.models.fields.DateTimeField', [], {}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'public_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'title': ('django.db.models.fields.CharField', [], {'max_length': '128'}) } } complete_apps = ['books']
执行
./manage.py migrate books
些时数据库中是最新的数据了!!!
*********************************************************************************************************
查看sqlite数据库中的内容
当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端,以sqlite>前缀标识:
[cdms@cdms LearnSouth]$ sqlite3 demo.db
查看数据库文件信息命令(注意命令前带字符'.'):
sqlite>.database
查看所有表的创建语句:
sqlite>.schema
*********************************************************************************************************
把修改后的模型合并到字段。
在老项目中使用
首先把south添加到install_apps变量中。
然后执行
./manage.py syncdb
之后执行转换,south会假装执行合并过程
./manage.py convert_to_south myapp
项目拷贝到其他机器上使用的过程中。如果修改提交。必须要执行
./manage.py migrate myapp 0001 --fake
如果没有数据库的项目,则不需要以上步骤。只需要执行syncdb同步到db。
数据迁移
在实践项目中,对模型的的修改往往伴随数据的修改。
比如原有的用户表采用了明文密码。在新版本中需要修改为加密的密码。通常开发过程中需要后台和db的配合。
后台修改字段,db部分在修改。
这里south的强大之处就出来了。可以在代码中修改迁移数据。
注意:一定要备份数据库
下面说说使用demo
首先建立1个app
./manager.py startapp book2
之后在模型里面添加数据
from django.db import models class User(models.Model): username = models.CharField(max_length=255) password = models.CharField(max_length=60) name = models.TextField()
初始化数据记录
./manage.py schemamigration --initial book2
合并到数据库
./manage.py migrate book2
添加数据
./manage.py shell User.objects.create(username="andrew", password="ihopetheycantseethis", name="Andrew Godwin")
以上步骤模拟了在实际过程中第一版的开发和数据导入。
接下来更新版本。密码修改为加密部分。
首先修改模型
from django.db import models # Create your models here. import hashlib class User(models.Model): username = models.CharField(max_length=255) # password = models.CharField(max_length=60) password_salt = models.CharField(max_length=8, null=True) password_hash = models.CharField(max_length=40, null=True) name = models.TextField() def check_password(self, password): return hashlib.sha1(self.password_salt + password).hexdigest() == self.password_hash
执行合并数据库
./manage.py schemamigration book2 --auto
下面是好玩的部分。新建1个数据迁移记录
./manage.py datamigration book2 hash_passwords
在 migrations文件夹下面生成1份0003_hash_password.py文件。
修改forwards函数。
def forwards(self, orm): "Write your forwards methods here." # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. import random, hashlib, string for user in orm.User.objects.all(): user.password_salt = "".join([random.choice(string.letters) for i in range(8)]) user.password_hash = hashlib.sha1(user.password_salt + user.password).hexdigest() user.save()
forwards函数里面。执行了修改密码,导入部分。
回退函数也修改
def backwards(self, orm): "Write your backwards methods here." raise RuntimeError("cannot reverse this migration.")
接下来删除models.py中原始的password字段。
执行合并
outekiMacBook-Air:LearnSouth watsy$ ./manage.py schemamigration book2 --auto /Users/watsy/Documents/code/gitoschina/python/LearnSouth/book2/models.py:7: DeprecationWarning: the sha module is deprecated; use the hashlib module instead import sha ? The field 'User.password' does not have a default specified, yet is NOT NULL. ? Since you are removing this field, you MUST specify a default ? value to use for existing rows. Would you like to: ? 1. Quit now, and add a default to the field in models.py ? 2. Specify a one-off value to use for existing columns now ? 3. Disable the backwards migration by raising an exception. ? Please select a choice: 2 ? Please enter Python code for your one-off default value. ? The datetime module is available, so you can do e.g. datetime.date.today() >>> "" - Deleted field password on book2.User Created 0004_auto__del_field_user_password.py. You can now apply this migration with: ./manage.py migrate book2
执行合并到数据库
outekiMacBook-Air:LearnSouth watsy$ ./manage.py migrate book2 /Users/watsy/Documents/code/gitoschina/python/LearnSouth/book2/models.py:7: DeprecationWarning: the sha module is deprecated; use the hashlib module instead import sha Running migrations for book2: - Migrating forwards to 0004_auto__del_field_user_password. > book2:0002_auto__add_field_user_password_salt__add_field_user_password_hash > book2:0003_hash_passwords - Migration 'book2:0003_hash_passwords' is marked for no-dry-run. > book2:0004_auto__del_field_user_password - Loading initial data for book2. Installed 0 object(s) from 0 fixture(s)
测试执行以后的数据库是否修改密码成功
outekiMacBook-Air:LearnSouth watsy$ ./manage.py shell >>> from book2.models import User >>> User.objects.get(id=1).check_password("258841679") True >>> User.objects.get(id=1).check_password("fakepass") False
测试修改成功~
相关推荐
django-mailbox 是一个能轻松、自动的从POP3, IMAP 或本地邮箱中导入邮件至 Django 应用程序中的工具。它读取的信息将存储在 Django 的模型数据库,你可以随意处理这些数据。 标签:django
允许用户从django管理站点中的CSV文件导入模型。 基本用法 要为模型启用CSV导入,只需将ImportCSVModelAdmin子类化,将其importer_class设置为ModelForm子类,然后将其注册为模型的ModelAdmin。 每一行CSV数据将被...
'django_aggtrigg',)在模型中导入字段: from django_aggtrigg.models import IntegerTriggerFieldfrom django_aggtrigg.models import FloatTriggerField按原样配置您的字段: class Apple(models.Model): indice ...
django-usda概述django-usda导入USDA国家营养标准参考数据库(SR22)并将其映射到Django模型。要求的Python 2.5.x Django 1.2.x(import_sr22不适用于早期版本)安装将usda文件夹复制到PYTHONPATH上的可用位置。 在...
Django数据向导是一种交互式工具,用于通过和将表格数据(例如...Django数据向导提供了一个, 和用于指定要导入的(例如,先前上传的文件),选择,映射数据和,以及(异步)将导入到任何目标中数据库中的模型。 数据
你可以通过导入django.db.connection对像来轻松实现,它代表当前数据库连接。 要使用它,需要通过connection.cursor()得到一个游标对像。 然后,使用cursor.execute(sql, [params])来执行SQL语句,使用cursor....
django-cities-light, 提供三个模型的简单应用程序 django-cities-light--- 简单的django城市替代品。...地区/州和数据库中的城市数据的模型和命令。数据从 GeoNames 提取并包含城市。地区/国家和国家/
在Django模型中创建表或从数据库导入。 python manage.py inspectdb> models.py将数据导入表。 ##配置MindsDB参见mindsdb / config.json python -m mindsdb --api = http,mysql --config = config.json \ INSERT ...
- 可能使用了数据分析库如pandas和numpy,以及机器学习库如scikit-learn或TensorFlow,用于处理数据和训练预测模型。 - 数据库技术可能用于存储和管理历史数据、预测结果等。 3. **功能特点**: - 系统支持数据的...
默沙东 ... S3 上 MSD-mapred 的输出被导入到 django 服务器的 postgres 数据库中。 MSD-android 是一个与 MSD-django 通信的安卓应用程序,用于获取歌曲配置文件并响应用户的年份预测和歌曲推荐请求。
用于其他验证的模型clean方法(有一些例外)。 服务-负责写入数据库的功能。 选择器-用于从数据库中获取数据的函数。 在Django中,业务逻辑不应存在于: API和视图。 序列化器和表格。 表单标签。 模型s
Django 快速同步一个 Django 应用程序,可帮助您使用原始 SQL 导入大量数据 Important: This library only supports Postgres (common for Django projects)!例子假设您在应用程序“预订”中有此模型: class ...
它导入到你的models.py和替换ImageField与ImageWithThumbsField在模型 添加一个sizes属性,其中包含要用于缩略图的尺寸列表 确保您在settings.py中定义了MEDIA_URL或STATIC_URL 就是这样! 工作实例 models.py ...
从django_db_views.db_view导入DBView 类Balance(DBView): virtual_card = models.ForeignKey( VirtualCard, on_delete=models.DO_NOTHING, related_name='virtual_cards' ) total_discount = models....
Google 登录方法: 安装 Docker ( )特征: 来自 Firebase 的带有 JWT 的 Django Rest Framework API(使用: : ) 从Django Admin导入/导出SQLite数据库(为简单起见) Svelte 前端 ( ) 基本 CRUD 应用示例自定义...
在使用单元测试时,有...对于已经有数据存在于正式数据库的app来说,使用Fixture 载入数据,是最简便有效的方法。 本文通过配置myapp.json,tests.py的实例代码给大家详细介绍。 基础配置 在settings.py 中配置如下
ODM2可以在这里找到: : 所有ODM2表都存在Django模型。 ODM2Core的表单和许多其他ODM2表。 实现了通过高图绘制测量结果值的图形。 只要正确设置数据记录器文件列和结果,就可以导入数据记录器文件。 其他ODM2工具...
> Django 框架主要关注的是模型(Model)、模板(Template)和视图(Views) 称为MTV模式 `Model` 存取数据 `View` 决定需要调取哪些数据 `Template` 负责将调取出的数据以合理的方式展现出来 需要思考两个问题...
本项目基于web框架的教师管理成绩系统使用基于web的django框架编写,采用了MTV的框架模式,即模型M,视图V和模版T,是从我们熟悉的 MVC 模式变化而来的。Model(模型):负责业务对象与数据库的对象(ORM)。Template...
数据: python从网易云热榜爬取歌曲信息,保存到数据库中 主要功能: 展示音乐信息,根据收藏进行推荐 ## 推荐算法思路 通过协调过滤计算和其他用户的距离,然后进行筛选。如果用户数量不足,推荐数目不够15条,就会...