B站网课:点击蓝色字体跳转
或者复制链接至浏览器访问:https://www.bilibili.com/video/BV1vK4y1o7jH?p=16&vd_source=597e21cf34ffcdce468ba00be2177e8a
添加字段之后要记得进行迁移,变化才能同步至数据库
python3 manage.py makemigrations
python3 manage.py migrate
class Author(models.Model):
name = models.CharField('姓名',max_length=11,default='')
age = models.IntegerField('年龄')
email = models.EmailField('邮箱')
python3 manage.py makemigrations
python3 manage.py migrate
进入mysql:
mysql -u root -p
输入密码:123456
use mysite3;
show tables;
desc bookstore_author;
null尽量不要使用
修改过字段选项【添加或更改】均要执行makemigrations和migrate
python3 manage.py makemigrations
python3 manage.py migrate
更改表名
class Book(models.Model):
title = models.CharField('书名',max_length=50,default='')
price = models.DecimalField('价格',max_digits=7,
decimal_places=2,default=0.0)
class Meta:
db_table='book'
添加内容:
class Meta:
db_table='book'
数据库的迁移
python3 manage.py makemigrations
python3 manage.py migrate
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField('书名',max_length=50,default='',unique=True)
pub = models.CharField('出版社',max_length=100,default='')
price = models.DecimalField('价格',max_digits=7,
decimal_places=2,default=0.0)
market_price = models.DecimalField('价格', max_digits=7,
decimal_places=2, default=0.0)
class Meta:
db_table='book'
class Author(models.Model):
name = models.CharField('姓名',max_length=11,default='')
age = models.IntegerField('年龄',default=1)
email = models.EmailField('邮箱',null=True)
class Meta:
db_table='author'
数据库的迁移
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py shell
from bookstore.models import Book
b1 = Book.objects.create(title='python',pub='清华大学出版社',price=20,market_price=25)
python3 manage.py shell
from bookstore.models import Book
b2 = Book(title='Django',pub='清华大学出版社',price=70,market_price=75)
b2.save()
(持续更新中)