django数据模型导⼊数据库⽅法提炼 官⽅⼿册⾥⾯的步骤
在新项⽬中使⽤
第⼀步创建项⽬
django-admin.py startproject LearnSouth
创建app
django-admin.py startapp books
在ings中修改数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'demo.db', # Or path to databa file if using sqlite3.
# The following ttings are not ud 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 = (
'ib.auth',
'ttypes',
'ib.ssions',
'ib.sites',武当山位于
'ssages',
'ib.staticfiles',
# Uncomment the next line to enable the admin:
# 'ib.admin',
# Uncomment the next line to enable admin documentation:
# 'ib.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):
秋天种什么花urname = 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(lf, orm):昭怎么读
式微什么意思# Adding model 'XiaoShuo'
(u'id', lf.gf('dels.fields.AutoField')(primary_key=True)),
('create_date', lf.gf('dels.fields.DateTimeField')()),
('public_date', lf.gf('dels.fields.DateTimeField')(auto_now=True, blank=True)), ('title', lf.gf('dels.fields.CharField')(max_length=128)),
('contnet', lf.gf('dels.fields.CharField')(max_length=512000)),
))
db.nd_create_signal(u'books', ['XiaoShuo'])
# Adding model 'Comment'
(u'id', lf.gf('dels.fields.AutoField')(primary_key=True)),
世界计量日('urname', lf.gf('dels.fields.CharField')(max_length=64)),
('email', lf.gf('dels.fields.CharField')(max_length=64)),
))
db.nd_create_signal(u'books', ['Comment'])
def backwards(lf, orm):
# Deleting model 'XiaoShuo'
db.delete_table(u'books_xiaoshuo')
# Deleting model 'Comment'
db.delete_table(u'books_comment')
models = {
t': {
愧疚的近义词
'Meta': {'object_name': 'Comment'},
'email': ('dels.fields.CharField', [], {'max_length': '64'}),
u'id': ('dels.fields.AutoField', [], {'primary_key': 'True'}),
'urname': ('dels.fields.CharField', [], {'max_length': '64'})
},
u'books.xiaoshuo': {
'Meta': {'object_name': 'XiaoShuo'},
'contnet': ('dels.fields.CharField', [], {'max_length': '512000'}),
'create_date': ('dels.fields.DateTimeField', [], {}),
u'id': ('dels.fields.AutoField', [], {'primary_key': 'True'}),
'public_date': ('dels.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'title': ('dels.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)一键u盘装系统
class Comment(models.Model):
urname = 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(lf, orm):
# Changing field 'ail'
db.alter_column(u'books_comment', 'email', lf.gf('dels.fields.EmailField')(max_length=75)) def backwards(lf, orm):
# Changing field 'ail'
db.alter_column(u'books_comment', 'email', lf.gf('dels.fields.CharField')(max_length=64))
models = {
t': {
'Meta': {'object_name': 'Comment'},
'email': ('dels.fields.EmailField', [], {'max_length': '75'}),
u'id': ('dels.fields.AutoField', [], {'primary_key': 'True'}),
'urname': ('dels.fields.CharField', [], {'max_length': '64'})
},
u'books.xiaoshuo': {
'Meta': {'object_name': 'XiaoShuo'},
'contnet': ('dels.fields.CharField', [], {'max_length': '512000'}),
'create_date': ('dels.fields.DateTimeField', [], {}),
u'id': ('dels.fields.AutoField', [], {'primary_key': 'True'}),
'public_date': ('dels.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'title': ('dels.fields.CharField', [], {'max_length': '128'})
}
}
complete_apps = ['books']
执⾏
./manage.py migrate books
些时数据库中是最新的数据了
*********************************************************************************************************
查看sqlite数据库中的内容
当前⽬录下建⽴或打开test.db数据库⽂件,并进⼊sqlite命令终端,以sqlite>前缀标识:[cdms@cdms LearnSouth]$ sqlite3 demo.db
查看数据库⽂件信息命令(注意命令前带字符'.'):
sqlite>.databa
查看所有表的创建语句:
sqlite>.schema
*********************************************************************************************************把修改后的模型合并到字段。
在⽼项⽬中使⽤
⾸先把south添加到install_apps变量中。
然后执⾏
./manage.py syncdb
之后执⾏转换,south会假装执⾏合并过程
./manage.py convert_to_south myapp
项⽬拷贝到其他机器上使⽤的过程中。如果修改提交。必须要执⾏
./manage.py migrate myapp 0001 --fake
如果没有数据库的项⽬,则不需要以上步骤。只需要执⾏syncdb同步到db。