建表语句
1.手动建数据库
2.在__init__文件中:
Import pymysql
pymysql.install_as_MySQLdb()
3.在models文件中:
from django.db import models # Create your models here. class Book(models.Model): id=models.AutoField(primary_key=True) name=models.CharField(max_length=32) price=models.DecimalField(max_digits=5,decimal_places=2) publish_date=models.DateField() publish=models.ForeignKey(to='Publish',to_field='id') authors=models.ManyToManyField(to='Author') class Publish(models.Model): id=models.AutoField(primary_key=True) name=models.CharField(max_length=32) city=models.CharField(max_length=32) class Author(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) age=models.IntegerField() author_detail=models.OneToOneField(to="AuthorDetail",to_field='id') class AuthorDetail(models.Model): id = models.AutoField(primary_key=True) # name = models.CharField(max_length=32) telephone=models.BigIntegerField() addr=models.CharField(max_length=32) email=models.EmailField(null=True)
4.settings中:
DATABASES = { 'ENGINE': 'django.db.backends.mysql', 'NAME':'0113', 'HOST':'127.0.0.1', 'PORT':'3306', 'USER': 'root', 'PASSWORD': 'root', } }
5.在迁移数据库
python manage.py makemigrations
python manage.py migrate