django之创建第8个项目-数据库配置及同步研究
1、sqlitestudio-2.1.5数据库可视化工具--百度云盘下载 2、编写C:\djangoweb\helloworld\blog\models.py文件
# Create your models here.
#coding:utf-8
from django.db import models
class Student(models.Model):
name=models.CharField(max_length=50)
age=models.IntegerField() 3、配置C:\djangoweb\helloworld\helloworld\setting.py的DATABASES变量
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2sqlite3', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'test.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.
}
} 4、同步数据库(之前创建数据库删掉)
c:\djangoweb\helloworld>manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table blog_student You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): no #是否创建超级用户?这里我选择了no
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
c:\djangoweb\helloworld> 5、C:\djangoweb\helloworld\blog\models.py文件新增intime数据类型
# Create your models here.
#coding:utf-8 from django.db import models
class Student(models.Model):
name=models.CharField(max_length=50)
age=models.IntegerField()
intime=models.DateTimeField() #新增 6、数据库同步,将新增的model中的内容更新到数据库中
【注意事项】
1)这里需要删除掉之前的数据库中对应的表再同步数据库(鼠标右键点击blog_student表,在下拉菜单中选择“删除表”),
2)尽量不要直接删除数据库,如果里面有数据那么就完蛋了
c:\djangoweb\helloworld>manage.py syncdb 7、反向同步数据库
1)在sqlite3可视化工具中添加新的数据类型,给student添加sex数据类型数据
2)新增teacher表,并创建id和name2个数据类型
3)执行反向同步数据库,manage.py inspectdb
4)效果:
c:\djangoweb\helloworld>manage.py inspectdb
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field na
s.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [a
name]'
# into your database.
from __future__ import unicode_literals from django.db import models class AuthGroup(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=80, unique=True)
class Meta:
db_table = 'auth_group' class AuthGroupPermissions(models.Model):
id = models.IntegerField(primary_key=True)
group_id = models.IntegerField()
permission = models.ForeignKey('AuthPermission')
class Meta:
db_table = 'auth_group_permissions' class AuthPermission(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
content_type_id = models.IntegerField()
codename = models.CharField(max_length=100)
class Meta:
db_table = 'auth_permission' class AuthUser(models.Model):
id = models.IntegerField(primary_key=True)
password = models.CharField(max_length=128)
last_login = models.DateTimeField()
is_superuser = models.BooleanField()
username = models.CharField(max_length=30, unique=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=75)
is_staff = models.BooleanField()
is_active = models.BooleanField()
date_joined = models.DateTimeField()
class Meta:
db_table = 'auth_user' class AuthUserGroups(models.Model):
id = models.IntegerField(primary_key=True)
user_id = models.IntegerField()
group = models.ForeignKey(AuthGroup)
class Meta:
db_table = 'auth_user_groups' class AuthUserUserPermissions(models.Model):
id = models.IntegerField(primary_key=True)
user_id = models.IntegerField()
permission = models.ForeignKey(AuthPermission)
class Meta:
db_table = 'auth_user_user_permissions' class BlogStudent(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
age = models.IntegerField()
intime = models.DateTimeField()
sex = models.IntegerField()
class Meta:
db_table = 'blog_student' class DjangoAdminLog(models.Model):
id = models.IntegerField(primary_key=True)
action_time = models.DateTimeField()
user = models.ForeignKey(AuthUser)
content_type = models.ForeignKey('DjangoContentType', null=True, blank=Tru object_id = models.TextField(blank=True)
object_repr = models.CharField(max_length=200)
action_flag = models.PositiveSmallIntegerField()
change_message = models.TextField()
class Meta:
db_table = 'django_admin_log' class DjangoContentType(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
app_label = models.CharField(max_length=100)
model = models.CharField(max_length=100)
class Meta:
db_table = 'django_content_type' class DjangoSession(models.Model):
session_key = models.CharField(max_length=40, unique=True)
session_data = models.TextField()
expire_date = models.DateTimeField()
class Meta:
db_table = 'django_session' class DjangoSite(models.Model):
id = models.IntegerField(primary_key=True)
domain = models.CharField(max_length=100)
name = models.CharField(max_length=50)
class Meta:
db_table = 'django_site' class Teacher(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
class Meta:
db_table = 'teacher'
c:\djangoweb\helloworld> 8、修改C:\djangoweb\helloworld\blog\models.py文件
# Create your models here.
#coding:utf-8
from django.db import models
"""
class Student(models.Model):
name=models.CharField(max_length=50)
age=models.IntegerField()
intime=models.DateTimeField()
"""
#修改为:
class Student(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
age = models.IntegerField()
intime = models.DateTimeField()
sex = models.IntegerField()
class Meta:
db_table = 'student' 9、删除“blog_student”表并同步数据库manage.py syncdb 10、将数据类型等信息输出到models.py文件中
1)manage.py inspectdb > blog/models.py
2)查看models文件内容(删除多余后的效果):
from django.db import models
class Student(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
age = models.IntegerField()
intime = models.DateTimeField()
sex = models.IntegerField()
class Meta:
db_table = 'student' class Teacher(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
class Meta:
db_table = 'teacher'
百度云盘:django之创建第8个项目-数据库配置及同步研究
django之创建第8个项目-数据库配置及同步研究的更多相关文章
- django之创建第8-3个项目-数据库数据提取之高级操作
1.配置test2.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- django之创建第8-1个项目-数据库之增删改查/数据库数据显示在html页面
1.为test.DB数据库预先创建下面数据 1 张三 16 2015-01-02 12 李四 17 2015-01-04 13 王五 14 ...
- django之创建第7-2个项目-url配置分离
1.urls.PY分离 # -*- coding: UTF-8 -*- from django.conf.urls import patterns, include, url # Uncomment ...
- django之创建第7个项目-url配置
1.配置urls.py from django.conf.urls import patterns, include, url #Uncomment the next two lines to ena ...
- django之创建第8-2个项目-数据库数据提取之过滤操作符相关
"""1)age__gt = 16等价于age > 162)age = 163)age__gte = 16等价于age >= 164)name__contai ...
- django之创建第7-1个项目-url配置高级
修改urls.PY文件 # -*- coding: UTF-8 -*- from django.conf.urls import patterns, include, url # Uncomment ...
- django之创建第11个项目-页面整合
目的:将如下众多html页面整合到一个index.html页面中. 百度云盘:django之创建第11个项目-页面整合 用下面的方式实现: <!DOCTYPE html> <head ...
- django之创建第10-1个项目-图片上传并记录上传时间
1.百度云盘:django之创建第10-1个项目-图片上传并记录上传时间 2.主要修改的配置文件有3个,forms.views和models3个文件以及html 3.forms.py文件修改 #cod ...
- django之创建第10个项目-图片上传方式1
1.upload.HTMl <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang=& ...
随机推荐
- Could not install packages due to an Environment Error: [Errno 13] Permission denied 解决方案
执行pip install 报错如下: Could not install packages due to an Environment Error: [Errno 13] Permission de ...
- 【转】搜狗开源内部项目管理平台Cynthia意欲何为
FROM : http://blog.csdn.net/dj0379/article/details/38356825 目前,在项目管理与缺陷管理系统上,中国的中小开发团队基本都在使用国外产品,在理念 ...
- @ZooKeeper注册中心安装(单节点)
1.下载zookeeper 下载地址:https://archive.apache.org/dist/zookeeper/,这里我们使用3.4.6版本. [winner-0715@localhost ...
- iOS:麦克风权限检测和获取
一.检测 该方法是用来判断麦克风是否进行过授权,如果授权过就直接进行需要的功能操作:如果没有进行授权,那么就要获取授权. AVAuthorizationStatus authStatus = [AVC ...
- 网关局域网通信协议V2.0
http://docs.opencloud.aqara.cn/development/gateway-LAN-communication/ https://github.com/aqara/openc ...
- [leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
- ubuntu 下python环境的切换使用
如何在Anaconda的python和系统自带的python之间切换 一般ubuntu下有三种python环境,1. 系统自带python2,3;在/usr/bin路径下:2. anaconda下安装 ...
- 如何在Windows版的ScaleIO的节点中添加磁盘
嗯, 为什么会有这个问题呢? 因为我要安装ScaleIO 1.32, 在使用Installation manager完成初始安装之后, 需要将一些磁盘添加到ScaleIO的storage pool中. ...
- Web项目MySQL配置文件运维
root@mysqltest:/etc/mysql/mysql.conf.d# cat mysqld.cnf # # The MySQL database server configuration f ...
- 深入理解FFM原理与实践
原文:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html 深入理解FFM原理与实践 del2 ...