django之创建第8个项目-数据库配置及同步研究

Posted Xiao|Deng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 Djangos auth system, which means you dont 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之创建第7-2个项目-url配置分离

django之创建第10-1个项目-图片上传并记录上传时间

django之创建第8-1个项目-数据库之增删改查/数据库数据显示在html页面

django之创建第7个项目-url配置

django之创建第12个项目-加载图片

django之创建第7-4个项目-配置views文件实现url传值