from django.db import models
# Create your models here.
class Question(models.Model):
#Create char type attribute in DB, which length is 200,name is question_text.
question_text = models.CharField(max_length=200)
#Create time type attribute in DB, which name is pub_date
pub_date = models.DateTimeField('date published')
生成的SQL代碼:
mysql> show create table polls_question;
| polls_question | CREATE TABLE `polls_question` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_text` varchar(200) NOT NULL,
`pub_date` datetime(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
class Choice(models.Model):
#創(chuàng)建外鍵鏈接到Question類中,并且設(shè)置級(jí)聯(lián)刪除
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
生成的SQL代碼:
mysql> show create table polls_choice;
| polls_choice | CREATE TABLE `polls_choice` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`choice_text` varchar(200) NOT NULL,
`votes` int(11) NOT NULL,
`question_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `polls_choice_question_id_c5b4b260_fk_polls_question_id` (`question_id`),
CONSTRAINT `polls_choice_question_id_c5b4b260_fk_polls_question_id` FOREIGN KEY (`question_id`) REFERENCES `polls_question` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
在將Model的數(shù)據(jù)庫表中的屬性處理好了之后再將app的配置類相對路徑添加到setting.py中的INSTALLED_APPS的列表中后執(zhí)行命令生成model:
[root@localhost mysite]# python manage.py makemigrations polls
Migrations for 'polls':
polls/migrations/0001_initial.py
- Create model Choice
- Create model Question
- Add field question to choice
檢查一下生成的SQL語句
[root@localhost mysite]# python manage.py sqlmigrate polls 0001
BEGIN;
--
-- Create model Choice
--
CREATE TABLE `polls_choice` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `choice_text` varchar(200) NOT NULL, `votes` integer NOT NULL);
--
-- Create model Question
--
CREATE TABLE `polls_question` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `question_text` varchar(200) NOT NULL, `pub_date` datetime(6) NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE `polls_choice` ADD COLUMN `question_id` integer NOT NULL;
ALTER TABLE `polls_choice` ADD CONSTRAINT `polls_choice_question_id_c5b4b260_fk_polls_question_id` FOREIGN KEY (`question_id`) REFERENCES `polls_question` (`id`);
COMMIT;
將對數(shù)據(jù)庫模型的修改寫入數(shù)據(jù)庫:
[root@localhost mysite]# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying polls.0001_initial... OK
到底干了些什么呢?
class 類名:
變量名=models.xxx(max_length=200)
SQL創(chuàng)建:
create table App名_類名 ;
添加 屬性 變量名 數(shù)據(jù)類型 ;
- 遷移非常強(qiáng)大饿肺,隨著時(shí)間的推移裸弦,您可以隨著時(shí)間的推移更改您的模型,而不需要?jiǎng)h除您的數(shù)據(jù)庫或表并創(chuàng)建新的模型 - 它專門從事現(xiàn)場升級(jí)數(shù)據(jù)庫,而不會(huì)丟失數(shù)據(jù)
python manage.py makemigrations
python manage.py migrate
利用python manage.py shell 執(zhí)行 API 指令:
from polls.models import Question, Choice
from django.utils import timezone
q = Question(question_text="What's new?", pub_date=timezone.now())
q.save()
q.id
>1
q.question_text
>"What's up?"
查看數(shù)據(jù)庫: 成功寫入
mysql> select * from polls_question;
+----+---------------+----------------------------+
| id | question_text | pub_date |
+----+---------------+----------------------------+
| 1 | what's up? | 2017-07-18 10:44:39.276937 |
+----+---------------+----------------------------+
1 row in set (0.00 sec)