Library management system, model creation
1, there is provided a database connection settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': "test", "USER": "root", "PASSWORD": "123321", "HOST": "127.0.0.1", "PORT" : "3306" , } }
2, to create a app02, and disposed inside settings.py; in app02 / models.py which model is as follows:
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 ) In Email = models.EmailField () # internal implementations are charField, defined EmailField to form validation class the Author (models.Model): name = models.CharField (MAX_LENGTH = 32 ) Age = models.IntegerField () # Add table relationship authordetail = models.OneToOneField (to = ' authorDetail ' ) # cutting table, because they do not put a single common data table, the regular the data also with a separate release sheet, to improve the efficiency of query class AuthorDetail (models.Model): addr = models.CharField (MAX_LENGTH = 32 ) Phone= models.IntegerField()
Makemigrations execution and migrate data synchronization, see the source code to create a model table:
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;