- 新建Django项目
- python3 manage.py startapp stu
- settings.py:
- import os
- includedapp add ‘stu’
- modify the database into mysql - 配置stu下的models.py
from django.db import models
# Create your models here.
# Post model device
class Post(models.Model):
pid = models.AutoField(primary_key=True)
title = models.CharField(max_length=100, unique=True)
content = models.TextField()
create = models.DateTimeField(auto_now_add=True)
modify = models.DateTimeField(auto_now=True)
email = models.EmailField()
isdelete = models.BooleanField(default=False)
access_count = models.PositiveIntegerField()
price = models.DecimalField(max_digits=5, decimal_places=2)
# object output
def __unicode__(self):
return u'Post:%s,%s'(self.title, self.access_count)
# customize table name
# try not to use it,avoiding the confusion of tables' name.
class Meta:
db_table = 't_post'
- init.py添加
import pymysql
pymysql.install_as_MySQLdb()
python命令创建迁移文件
python3 manage.py makemigrations stu
python命令执行迁移文件
python3 manage.py migrate
- 创建admin
stu包下的admin.py
from django.contrib import admin
from .models import *
# Register your models here.
admin.site.register(Post)
terminal命令行
python3 manage.py createsuperuser
- 单表操作:修改models.py添加可以上传文件功能
from django.db import models
# Create your models here.
# Post model device
class Post(models.Model):
pid = models.AutoField(primary_key=True)
title = models.CharField(max_length=100, unique=True)
content = models.TextField()
create = models.DateTimeField(auto_now_add=True)
modify = models.DateTimeField(auto_now=True)
email = models.EmailField()
isdelete = models.BooleanField(default=False)
access_count = models.PositiveIntegerField()
price = models.DecimalField(max_digits=5, decimal_places=2)
file = models.ImageField(upload_to='upload/images/')
# object output
def __unicode__(self):
return u'Post:%s,%s'(self.title, self.access_count)
# customize table name
# try not to use it,avoiding the confusion of tables' name.
class Meta:
db_table = 't_post'
删除迁移文件0001_initial.py
hadoop@hadoop:~$ mysql -uroot -phadoop
mysql> use DjangoModels;
mysql> show tables;
mysql> select * from django_migrations
mysql> delete from django_migrations where app=‘stu’;
mysql> select * from django_migrations;
mysql> show tables;
mysql> drop table t_post;
mysql> show tables;
重新创建迁移文件stu,执行迁移文件
python3 manage.py makemigrations stu
python3 manage.py migrate
- 多表操作
models.dy
from django.db import models
# Create your models here.
# Post model device
class Post(models.Model):
pid = models.AutoField(primary_key=True)
title = models.CharField(max_length=100, unique=True)
content = models.TextField()
create = models.DateTimeField(auto_now_add=True)
modify = models.DateTimeField(auto_now=True)
email = models.EmailField()
isdelete = models.BooleanField(default=False)
access_count = models.PositiveIntegerField()
price = models.DecimalField(max_digits=5, decimal_places=2)
file = models.ImageField(upload_to='upload/images/')
# object output
def __unicode__(self):
return u'Post:%s,%s'(self.title, self.access_count)
# customize table name
# try not to use it,avoiding the confusion of tables' name.
class Meta:
db_table = 't_post'
class Student(models.Model):
sno = models.AutoField(primary_key=True)
sname = models.CharField(max_length=30)
def __unicode__(self):
return u'Student:%s'(self.sname)
class Scard(models.Model):
student = models.OneToOneField(Student, primary_key=True, on_delete=models.CASCADE)
major = models.CharField(max_length=30,unique=True)
def __unicode__(self):
return u'Scard:%s' % self.major
因为不涉及表内改动
只是增加了两个表
所以不用删除迁移文件重新创建一次
重新创建迁移文件stu,执行迁移文件
python3 manage.py makemigrations stu
python3 manage.py migrate
- 数据库设计表1vs1 1vs2 2vs2