资讯专栏INFORMATION COLUMN

python第三方库之Django学习笔记二

tomorrowwu / 1568人阅读

摘要:上一节项目框架已经搭建完毕,现在开始连接数据库,创建数据库设置默认安装了数据库打开文件数据库引擎数据库的名字小贴士如果你选择,数据库是以文件的形式生成,要设置成绝对路径创建表结构创建模型激活模型执行命令执行成功后目录结构如下图

上一节项目框架已经搭建完毕,现在开始连接数据库,创建model

1、数据库设置
python默认安装了sqlite数据库

打开文件:dayang/settings.py

ENGINE – 数据库引擎

     "django.db.backends.sqlite3", 
     "django.db.backends.postgresql", 
     "django.db.backends.mysql",
     "django.db.backends.oracle".

NAME – 数据库的名字

小贴士:如果你选择sqlite,数据库是以文件的形式生成,name要设置成绝对路径

By default, INSTALLED_APPS contains the following apps, all of which come with Django:

django.contrib.admin – The admin site. You’ll use it shortly.

django.contrib.auth – An authentication system.

django.contrib.contenttypes – A framework for content types.

django.contrib.sessions – A session framework.

django.contrib.messages – A messaging framework.

django.contrib.staticfiles – A framework for managing static files.

These applications are included by default as a convenience for the common case.

Some of these applications make use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:
manage.py migrate # 创建表结构

2、创建模型

3、激活模型
That small bit of model code gives Django a lot of information. With it, Django is able to:

Create a database schema (CREATE TABLE statements) for this app.
Create a Python database-access API for accessing Question and Choice objects.
But first we need to tell our project that the polls app is installed.

执行命令:manage.py makemigrations polls
# By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.

执行成功后目录结构如下图:

Now, run migrate again to create those model tables in your database:

remember the three-step guide to making model changes:

Change your models (in models.py).
Run python manage.py makemigrations to create migrations for those changes
Run python manage.py migrate to apply those changes to the database.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/110807.html

相关文章

  • python三方库之Django学习笔记

    摘要:上一节项目框架已经搭建完毕,现在开始连接数据库,创建数据库设置默认安装了数据库打开文件数据库引擎数据库的名字小贴士如果你选择,数据库是以文件的形式生成,要设置成绝对路径创建表结构创建模型激活模型执行命令执行成功后目录结构如下图 上一节项目框架已经搭建完毕,现在开始连接数据库,创建model 1、数据库设置python默认安装了sqlite数据库 打开文件:dayang/settings...

    Java3y 评论0 收藏0
  • python三方库之Django学习笔记

    摘要:一个空文件,告诉该目录是一个包。运行项目切换到目录执行命令浏览器输入看到以下界面运行成功小贴士修改端口命令创建切换到同级目录,执行命令现在的结构目录如下编写你的第一个视图新建文件运行命令查看运行结果 1、安装Django pip install Django 2、版本号查询 python -m django --version showImg(https://segmentfa...

    canger 评论0 收藏0
  • 保姆级教程带你开发优质的Python库之上篇【建议收藏】

    摘要:正式的专栏第篇,同学站住,别错过这个从开始的文章前面学委的入门到精通专栏积累了篇文章,当然学委博客还有几十篇应用的文章。 正式的Python专栏第9篇,同学站住...

    hyuan 评论0 收藏0
  • django rest framework个人学习笔记)————Quickstart

    摘要:我们将会创建一个简单的,来让管理员能够查看和编辑和。我们将会在接下来的例子中使用这个账户。创建一个新的模块叫。但是是一个好的设计。最后,我们使用来导入默认的登录登出这是可选的。我们想启用分页和只有用户能够调用。 Quickstart 我们将会创建一个简单的API,来让管理员能够查看和编辑Users和Groups。 项目开始 创建一个新的项目叫 tutorial,然后创建一个app叫qu...

    gghyoo 评论0 收藏0
  • django rest framework个人学习笔记)————Quickstart

    摘要:我们将会创建一个简单的,来让管理员能够查看和编辑和。我们将会在接下来的例子中使用这个账户。创建一个新的模块叫。但是是一个好的设计。最后,我们使用来导入默认的登录登出这是可选的。我们想启用分页和只有用户能够调用。 Quickstart 我们将会创建一个简单的API,来让管理员能够查看和编辑Users和Groups。 项目开始 创建一个新的项目叫 tutorial,然后创建一个app叫qu...

    JiaXinYi 评论0 收藏0

发表评论

0条评论

tomorrowwu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<