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):
#创建外键链接到Question类中,并且设置级联删除
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的数据库表中的属性处理好了之后再将app的配置类相对路径添加到setting.py中的INSTALLED_APPS的列表中后执行命令生成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;
将对数据库模型的修改写入数据库:
[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创建:
create table App名_类名 ;
添加 属性 变量名 数据类型 ;
- 迁移非常强大,随着时间的推移,您可以随着时间的推移更改您的模型,而不需要删除您的数据库或表并创建新的模型 - 它专门从事现场升级数据库,而不会丢失数据
python manage.py makemigrations
python manage.py migrate
利用python manage.py shell 执行 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?"
查看数据库: 成功写入
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)