django图书管理系统模型创建

图书管理系统, 模型创建

1、在settings.py里面设置数据库连接

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "test",
        "USER": "root",
        "PASSWORD": "123321",
        "HOST": "127.0.0.1",
        "PORT" : "3306" ,
    }
}

2、创建一个app02, 并在settings.py里面设置; 在app02/models.py里面模型如下:

from django.db import models

# Create your models here.

class Book(models.Model):
    name = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    publish_date = models.DateTimeField( auto_now_add= True)
    #添加表关联关系
    publish = models.ForeignKey( to='Publish' )
    authors = models.ManyToManyField( to='Author')


class Publish( models.Model):
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    email = models.EmailField()  #内部实现也是charField,定义为EmailField是为了表单校验

class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    #添加表关联关系
    authordetail = models.OneToOneField( to='AuthorDetail')


#切表,是因为不常用的数据单独放一张表, 对经常用的数据也单独放一张表,提高查询的效率
class AuthorDetail(models.Model):
    addr = models.CharField(max_length=32)
    phone = models.IntegerField()

执行makemigrations和migrate同步数据,查看创建模型表的源码:

D:\kindeditor_pro>python manage.py sqlmigrate app02 0001
BEGIN;
--
-- Create model Author
--
CREATE TABLE `app02_author` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(32) NO
T NULL, `age` integer NOT NULL);
--
-- Create model AuthorDetail
--
CREATE TABLE `app02_authordetail` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `addr` varchar(
32) NOT NULL, `phone` integer NOT NULL);
--
-- Create model Book
--
CREATE TABLE `app02_book` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(32) NOT
NULL, `price` numeric(8, 2) NOT NULL, `publish_date` datetime(6) NOT NULL);
CREATE TABLE `app02_book_authors` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `book_id` integ
er NOT NULL, `author_id` integer NOT NULL);
--
-- Create model Publish
--
CREATE TABLE `app02_publish` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(32) N
OT NULL, `addr` varchar(64) NOT NULL, `email` varchar(254) NOT NULL);
--
-- Add field publish to book
--
ALTER TABLE `app02_book` ADD COLUMN `publish_id` integer NOT NULL;
--
-- Add field authordetail to author
--
ALTER TABLE `app02_author` ADD COLUMN `authordetail_id` integer NOT NULL UNIQUE;
ALTER TABLE `app02_book_authors` ADD CONSTRAINT `app02_book_authors_book_id_55aef806_fk_app02_book_i
d` FOREIGN KEY (`book_id`) REFERENCES `app02_book` (`id`);
ALTER TABLE `app02_book_authors` ADD CONSTRAINT `app02_book_authors_author_id_191af2fa_fk_app02_auth
or_id` FOREIGN KEY (`author_id`) REFERENCES `app02_author` (`id`);
ALTER TABLE `app02_book_authors` ADD CONSTRAINT `app02_book_authors_book_id_author_id_1a895ee1_uniq`
 UNIQUE (`book_id`, `author_id`);
ALTER TABLE `app02_book` ADD CONSTRAINT `app02_book_publish_id_79be7951_fk_app02_publish_id` FOREIGN
 KEY (`publish_id`) REFERENCES `app02_publish` (`id`);
ALTER TABLE `app02_author` ADD CONSTRAINT `app02_author_authordetail_id_2bc028c9_fk_app02_authordeta
il_id` FOREIGN KEY (`authordetail_id`) REFERENCES `app02_authordetail` (`id`);
COMMIT;

猜你喜欢

转载自www.cnblogs.com/harryTree/p/11892373.html