python django官方教程手把手:如何连接数据库?

2022-09-1810:52:01后端程序开发Comments976 views字数 524阅读模式

1 执行数据库迁移,Migration, 成功后应该可以查看数据库

py manage.py migrate

python django官方教程手把手:如何连接数据库?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

打开数据库,可以看到表已经建立文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

python django官方教程手把手:如何连接数据库?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

数据库配置文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

python django官方教程手把手:如何连接数据库?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

2创建模型

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerFiel

3 将模型生成到数据库

3.1 将应用添加到主配置

python django官方教程手把手:如何连接数据库?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

3.2 执行迁移

py manage.py makemigrations polls

python django官方教程手把手:如何连接数据库?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

3.3 生成迁移sql语句

py manage.py sqlmigrate polls 0001

python django官方教程手把手:如何连接数据库?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

3.4 再次运行migrate

py manage.py migrate

python django官方教程手把手:如何连接数据库?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

作者:三重罗生门
来源:稀土掘金文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27794.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/27794.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定