Django是符合MVC架构的,这里现学习M—Model,而且Django自带了一个管理model(数据库)的界面,所以一并学习。

Database 配置

编辑Django的配置文件settings.py进行配置

添加polls app,修改后如下

INSTALLED_APPS = [
'django.contrib.admin',      # 管理界面
'django.contrib.auth',       # 认证系统
'django.contrib.contenttypes',  # 框架的content type
'django.contrib.sessions',     # session framework
'django.contrib.messages',     # messages framework
'django.contrib.staticfiles',   # 管理静态文件的framework
'polls.apps.PollsConfig',     # 我们自己的app
]

最后一行位新添加的,表示新增一个app,类polls.apps.PoolsConfig定义了app,名称为“polls”(可以打开这个类看到)。

还可以看到很多其他的app,我们之前说过,一个project可以有多个app,一个app可以属于多个project,这个的实现方式就是这样,每个app都位于不同的包下面,如果一个project想包含一个app,只需要把这个app的包的配置写在这儿就可以了。

接下来配置数据库,修改完之后如下

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'polls.db',
}
}

Django支持大多数主流的数据库,包括postgresql,mysql,sqlite等,这里为了简单就直接用sqlite,如果使用mysql应该配置成如下(其他类似)

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '',
}
}

数据库至此配置完成,接下来就是创建model

创建model

编辑mysite/polls/models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.
class Question(models.Model):
   # CharField:字段是字符串类型,有一个必要参数——字符串长度
question_text = models.CharField(max_length=200)
   # DateField:字段是日期类型
publ_date = models.DateField('date published')     def __unicode__(self):
        return self.question_text class Choice(models.Model):
   # question作为choice的外键,这里是多对一关系,删除的时候进行级联,Django还支持:many-to-one, many-to-many, and one-to-one.
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)     def __unicode__(self):
        return self.choice_text

每个model继承自models.Model,会继承一些常用的操作方法。每个class对应的属性都对应数据库中表中的字段——这也就是ORM

生成数据库表结构

# 告诉Django model改变了,并且保存为一个migration
python manage.py makemigrations

运行该命令,输出如下内容,并在pools/migrations下生成0001_initial.py

Migrations for 'polls':
0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice

我们可以看看Django根据这个migration会怎么生成表结构运行

# 0001 为之前生成的0001_initial.py的前缀,表示第一个版本的migration
python manage.py sqlmigrate polls 0001

输出

BEGIN;
--
-- Create model Choice
--
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
--
-- Create model Question
--
CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_test" varchar(200) NOT NULL, "publ_date" date NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE "polls_choice" RENAME TO "polls_choice__old";
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id"));
INSERT INTO "polls_choice" ("choice_text", "votes", "id", "question_id") SELECT "choice_text", "votes", "id", NULL FROM "polls_choice__old";
DROP TABLE "polls_choice__old";
CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id"); COMMIT;

Django会运行上面这些语句来生成数据库表结构,我们可以看到生成两张表分别对应两个model,给question这张表添加一个外键。

现在我们可以生成数据库表结构了,这个命令就是将上面生成的migration应用到数据库

python manage.py migrate

输出

Operations to perform:
Apply all migrations: admin, contenttypes, polls, auth, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying polls.0001_initial... OK
Applying sessions.0001_initial... OK

到这儿,数据库表结构生成完毕,我们可以使用sqlite3查看生成的表结构

sqlite3 polls.db
#以下为输出内容
SQLite version 3.8.7.1 2014-10-29 13:59:56
Enter ".help" for usage hints.
sqlite> .tables
auth_group django_admin_log
auth_group_permissions django_content_type
auth_permission django_migrations
auth_user django_session
auth_user_groups polls_choice
auth_user_user_permissions polls_question

使用API操作model

# 这里用的是ipython,进入django命令行
python manage.py shell
Python 2.7.9 (default, Mar 1 2015, 18:22:53)
Type "copyright", "credits" or "license" for more information. IPython 2.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
# 导入model
In [2]: from polls.models import Choice, Question
# 查看所有的question对象
In [3]: Question.objects.all()
Out[3]: [<Question: what`s up?>]
# 获取id为0的对象,没有id为0的对象,所以会报错
In [5]: Question.objects.get(id=0)
---------------------------------------------------------------------------
DoesNotExist Traceback (most recent call last)
<ipython-input-5-bd4c3a1273f2> in <module>()
----> 1 Question.objects.get(id=0) /usr/local/lib/python2.7/dist-packages/Django-1.9.7-py2.7.egg/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
120 def create_method(name, method):
121 def manager_method(self, *args, **kwargs):
--> 122 return getattr(self.get_queryset(), name)(*args, **kwargs)
123 manager_method.__name__ = method.__name__
124 manager_method.__doc__ = method.__doc__ /usr/local/lib/python2.7/dist-packages/Django-1.9.7-py2.7.egg/django/db/models/query.pyc in get(self, *args, **kwargs)
385 raise self.model.DoesNotExist(
386 "%s matching query does not exist." %
--> 387 self.model._meta.object_name
388 )
389 raise self.model.MultipleObjectsReturned( DoesNotExist: Question matching query does not exist.
# 获取id为1的对象
In [6]: Question.objects.get(id=1)
Out[6]: <Question: what`s up?> In [7]: q = Question.objects.get(id=1)
# 删除该对象
In [8]: q.delete()
Out[8]: (4, {u'polls.Choice': 3, u'polls.Question': 1})
# 查看发现已经删除
In [9]: Question.objects.all()
Out[9]: [] In [11]: from django.utils import timezone
# 新建一个对象
In [13]: q = Question(question_text="What's new?", publ_date=timezone.now())
# 保存到数据库
In [15]: q.save()
# 保存到数据库之后Django自动生成了id,只有在save之后才有
In [16]: q.id
Out[16]: 2
# 查看字段值
In [17]: q.question_text
Out[17]: "What's new?" In [18]: q.publ_date
Out[18]: datetime.datetime(2016, 7, 10, 13, 12, 40, 146050, tzinfo=<UTC>) In [19]: q.question_text = "What's up?" In [20]: q.save() In [22]: q
Out[22]: <Question: What's up?> In [23]: Question.objects.all()
Out[23]: [<Question: What's up?>] In [24]: Question.objects.filter(id=1)
Out[24]: []
# 使用filter查询
In [25]: Question.objects.filter(id=2)
Out[25]: [<Question: What's up?>] In [26]: Question.objects.filter(question_text__startswith='What')
Out[26]: [<Question: What's up?>] In [29]: Question.objects.get(publ_date__year=timezone.now().year)
Out[29]: <Question: What's up?>
# 查看question关联的choice
In [30]: q.choice_set.all()
Out[30]: []
# 新建一个choice关联到question
In [31]: q.choice_set.create(choice_text='Not much', votes=0)
Out[31]: <Choice: Not much> In [32]: q.choice_set.create(choice_text='The sky', votes=0)
Out[32]: <Choice: The sky> In [33]: c = q.choice_set.create(choice_text='Just hacking again', votes=0)
# 查看choice对应的question
In [34]: c.question
Out[34]: <Question: What's up?> In [35]: q.choice_set.all()
Out[35]: [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>] In [36]: q.choice_set.count()
Out[36]: 3

Django提供了很多API来操作model,包括新建、更新、删除、关联、从一个对象查询关联的对象等等,除此之外Django还提供了web界面对model进行管理。

Django Admin

使用以下命令依次输入用户名、邮箱(注意邮箱)、密码(8位以上的字母数字组成)

python manage.py createsuperuser

访问http://127.0.0.1:8000/admin/登陆,可以看到管理界面,但是现在并没有mode,接下来注册model

编辑polls/admin.py

from django.contrib import admin
from polls.models import Choice, Question # Register your models here.
admin.site.register(Choice)
admin.site.register(Question)

再次登陆就可以看到model:Choice,Question,可以在界面上增删改查,so easy


代码位置

http://pan.baidu.com/s/1dFshtXB

frist Django app— 二、 Model和管理界面的更多相关文章

  1. frist Django app — 一、 创建工程

    缘起 既然python都学了,学习python的时候感觉是相见恨晚,一种新的编程语言带给我一种新的思考问题的方式,为了巩固学过的东西并进一步学习python,就想学学Django,看看会不会带给我关于 ...

  2. frist Django app — 一、 创建工程(转载)

    转载地址:https://www.cnblogs.com/sunshine-2015/p/5658283.html 缘起 既然python都学了,学习python的时候感觉是相见恨晚,一种新的编程语言 ...

  3. 【django小练习之主机管理界面】

    需求: 利用django,js,bootstrap等实现登录,主机管理等操作. 实现截图 登录界面 主机界面,添加及编辑 部门管理界面 代码实现 目录层级 settings.py "&quo ...

  4. 使用django的admin的后台管理界面

    django的admin后台管理界面是方便我们对数据库操作的  是一个在浏览器显示的  图形化界面数据库操作 我们先在django中的admin中把我们需要在图形化界面中进行操作的表导入进去: 先把m ...

  5. Django App(二) Connect Mysql & defualt App admin

    这一篇接着上一篇polls App自动创建admin app.     1.安装数据库 这里的内容从官网看越看越像 EntityFramework的内容.Python支持SQLite,MySql,Or ...

  6. frist Django app — 三、 View

    前面已经说过了Django中model的一些用法,包括orm,以及操作的api,接下来就是搭一些简单的界面学习view——Django中的view.主要介绍以下两个方面: url映射 请求处理 模板文 ...

  7. frist Django app — 四、 完善View

    上一篇已经完成了polls的基本功能,接下来完善剩下的vote功能和并使用generic views改进请求处理view.包含表单的简单运用和前后台参数传递. 目录 vote:完善投票功能 gener ...

  8. frist Django app — 五、Test

    Test——很重要但是没有被重视起来的一个环节,至少是我自己,其实自己之前在做java web的时候就去尝试过怎么做REST接口的测试,一直没有找到一种合适方式,而且因为时间紧没有进一步深究,但是造成 ...

  9. django管理界面使用与bootstrap模板使用

    一.bootstrap模板使用 1.去bootstrap官网找一个合适的模板,下载下来,右键另存为即可 bootstrap官网---->bootstrap中文文档3-------->起步- ...

随机推荐

  1. github 出现 Permission denied (publickey)

    首先,清除所有的key-pairssh-add -Drm -r ~/.ssh删除你在github中的public-key 用下面的命令生成public key $ ssh-keygen -t rsa ...

  2. [ZJOI2004]嗅探器

    题目概要: 在无向图中寻找出所有的满足下面条件的点:割掉这个点之后,能够使得一开始给定的两个点a和b不连通,割掉的点不能是a或者b.(ZJOI2004) 数据范围约定结点个数N≤100边数M≤N*(N ...

  3. [硬件]_ELVE_STLINK下载出现nternal command error问题

    我之前也出现过这个这个,然后折腾一晚上,升级什么都都不好使 最后我换了一根短的线,回归正常!!!

  4. LEB128相关知识

    LEB128相关知识 介绍 LEB128(little endian base 128)是一种变长的整数压缩编码形式,它是出自于DWARF debug file format.在Android的Dal ...

  5. 编写一个简单的基于jmespath 的prometheus exporter

    目的很简单,因为系统好多监控指标是通过json 暴露的,并不是标准的prometheus metrics 格式,处理方法 实际上很简单,我们可以基于jsonpath 解析json数据,转换为prome ...

  6. C 函数传参问题

    1. 传指针 传入指针时会改变指针指向的内容 2. 传变量 传入变量,不会改变变量的内容,只是使用变量的数据

  7. Eclipse Memory Analyzer 分析内存泄露

    OutOfMemoryError示例 代码 package com.walson.heap; import java.util.ArrayList;import java.util.List; /** ...

  8. IO练习

    #IO操作 import time; fileObj = open('log.txt','a'); while(True): data = input('请输入要写入的内容 : '); if data ...

  9. openFileOutput和openFileInput还有FileOutStream与openFileOutput

    FileOutputStream fos = openFileOutput("",MODE_PRIVATE); fos.write("private".getB ...

  10. meter命令行模式运行,实时获取压测结果 (没试过 说不定以后要用)

    jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式 上面一条命令应该可以满足大部分需求. 使用-R指定节点时,当然要首先在这些节点上启动jmeter-serv ...