一、django创建数据库与表
Django类EF的CodeFisrt :
- 编辑
models.py
文件,改变模型。 - 运行
python manage.py makemigrations
为模型的改变生成迁移文件。 - 运行
python manage.py migrate
来应用数据库迁移。
models.py:
1 from django.db import models 2 3 class Question(models.Model): 4 question_text = models.CharField(max_length=200) 5 pub_date = models.DateTimeField('date published') 6 7 class Choice(models.Model): 8 question = models.ForeignKey(Question, on_delete=models.CASCADE) 9 choice_text = models.CharField(max_length=200) 10 votes = models.IntegerField(default=0)
setting.py:
1 INSTALLED_APPS = [ 2 # 添加自定义配置 3 'polls.apps.PollsConfig', 4 'django.contrib.admin', 5 'django.contrib.auth', 6 'django.contrib.contenttypes', 7 'django.contrib.sessions', 8 'django.contrib.messages', 9 'django.contrib.staticfiles', 10 ]
https://docs.djangoproject.com/zh-hans/3.2/intro/tutorial02/ 可查看django文档