Skip to content

Commit 13ca97c

Browse files
committed
翻译了第三章的一部分,只能翻译到这里了,后面的看不了了%>_<%
1 parent 735e45f commit 13ca97c

File tree

1 file changed

+85
-32
lines changed

1 file changed

+85
-32
lines changed

第三章-模型.md

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,111 @@
11
第三章 模型
22
-----------
33

4-
本章,我们将讨论以下话题:
4+
In this chapter, we will discuss the following topics:
5+
本章,我们将讨论以下话题:
56

6-
模型的重要性
7-
类图表
8-
模型结构模式
9-
模型行为模式
10-
迁移
7+
>The importance of models
8+
>模型的重要性
9+
10+
>Class diagrams
11+
>类图表
12+
13+
>Model structural patterns
14+
>模型结构模式
15+
16+
>Model behavioral patterns
17+
>模型行为模式
18+
19+
>Migrations
20+
>迁移
1121
1222
## M比V和C都更大
13-
在Django中,模型是具有处理数据库的一种面向对象的方法的类。通常,每个类都引用一个数据库表,,每个属性都引用一个数据库列。你可以使用自动生成的API查询这些表。
23+
In Django, models are classes that provide an object-oriented way of dealing with databases. Typically, each class refers to a database table and each attribute refers to a database column. You can make queries to these tables using an automatically generated API.
24+
在Django中,模型是具有处理数据库的一种面向对象的方法的类。通常,每个类都引用一个数据库表,每个属性都引用一个数据库列。你可以使用自动生成的API查询这些表。
25+
26+
Models can be the base for many other components. Once you have a model, you can rapidly derive model admins, model forms, and all kinds of generic views. In each case, you would need to write a line of code or two, just so that it does not seem too magical.
27+
模型是很多其他组件的基础。只要你有一个模型,你可以快速地推导模型admin,模型表单,以及所有类型的通用视图。在每个例子中,你都需要下一个行或是两行代码,这样可以让它看上去没有太多魔法。
28+
29+
Also, models are used in more places than you would expect. This is because Django can be run in several ways. Some of the entry points of Django are as follows:
30+
模型也被用在更多超出你期望的地方。这书因为Django可以以多种方法运行。Django的一些切入点如下:
1431

15-
模型是很多其他组件的基础。只要你有一个模型,你可以快速地推导模型admin,模型表单,以及所有类型的通用视图。在每个例子中,你都需要下一个行或是两行代码,这样可以让它看上去没有太多魔法。
32+
>The familiar web request-response flow
33+
>熟悉web请求-响应流程
1634
17-
模型也被用在更多超出你期望的地方。这书因为Django可以以多种方法运行。Django的一些切入点如下:
35+
>Django interactive shell
36+
>Django的交互式shell
1837
19-
熟悉web请求-响应流程
20-
Django的交互式shell
21-
管理命令
22-
测试脚本
23-
异步任务队列比如Celery
38+
>Management commands
39+
>管理命令
2440
25-
在多数的这些例子中,模型模块要导入(作为django.setup()的一部分)。因此,最好保证模型远离任何不必要的依赖,或者导入任何的其他Django组件,比如视图。
41+
>Test scripts
42+
>测试脚本
2643
27-
简而言之,恰当地设计模型是很重要的事情。现在,让我们从SuperBook模型设计开始。
44+
>Asynchronous task queues such as Celery
45+
>异步任务队列比如Celery
46+
47+
In almost all these cases, the model modules would get imported (as a part of django.setup()). Hence, it is best to keep your models free from any unnecessary dependencies or to import any other Django components such as views.
48+
在多数的这些例子中,模型模块要导入(作为django.setup()的一部分)。因此,最好保证模型远离任何不必要的依赖,或者导入任何的其他Django组件,比如视图。
49+
50+
In short, designing your models properly is quite important. Now let's get started with the SuperBook model design.
51+
简而言之,恰当地设计模型是很重要的事情。现在,让我们从SuperBook模型设计开始。
2852

2953
### 注释
30-
**自带午餐便当**
31-
*作者注释:SuperBook项目的进度会以这样的一个盒子出现。你可以跳过这个盒子,但是在web应用项目中的情况下,你缺少的是领悟,经验 。
54+
**自带午餐便当**
55+
Author's Note: The progress of the SuperBook project will appear in a box like this. You may skip the box but you will miss the insights, experiences, and drama of working in a web application project.
56+
*作者注释:SuperBook项目的进度会以这样的一个盒子出现。你可以跳过这个盒子,但是在web应用项目中的情况下,你缺少的是领悟,经验 。
3257

33-
史蒂夫和客户的第一周——超级英雄情报监控(简称为S.H.I.M)。简单来说,这是一个大杂烩。办公室是非常未来化的,但是不论做什么事情都需要上百个审核和签字。
58+
Steve's first week with his client, the SuperHero Intelligence and Monitoring or S.H.I.M. for short, was a mixed bag. The office was incredibly futuristic but getting anything done needed a hundred approvals and sign-offs.
59+
史蒂夫和客户的第一周——超级英雄情报监控(简称为S.H.I.M)。简单来说,这是一个大杂烩。办公室是非常未来化的,但是不论做什么事情都需要上百个审核和签字。
3460

35-
作为Django开发者的领队,史蒂夫已经配置好了中型的运行超过两天的4台虚拟机。第二天的一个早晨,机器自己不翼而飞了。一个附近的清洁机器人说,机器被法务部们给带走了,他们要对未经审核的软件安装做出处理。
61+
Being the lead Django developer, Steve had finished setting up a mid-sized development server hosting four virtual machines over two days. The next morning, the machine itself had disappeared. A washing machine-sized robot nearby said that it had been taken to the forensic department due to unapproved software installations.
62+
作为Django开发者的领队,史蒂夫已经配置好了中型的运行超过两天的4台虚拟机。第二天的一个早晨,机器自己不翼而飞了。一个附近的清洁机器人说,机器被法务部们给带走了,他们要对未经审核的软件安装做出处理。
3663

37-
然而,CTO哈特给予史蒂夫了极大的帮助。他要求机器在一个小时之内完好无损地给还回去。他还对SuperBook项目做出了提前审核以避免将来可能出现的任何阻碍。
64+
The CTO, Hart was, however, of great help. He asked the machine to be returned in an hour with all the installations intact. He had also sent pre-approvals for the SuperBook project to avoid any such roadblocks in future.
65+
然而,CTO哈特给予史蒂夫了极大的帮助。他要求机器在一个小时之内完好无损地给还回去。他还对SuperBook项目做出了提前审核以避免将来可能出现的任何阻碍。
3866

39-
那个下午的稍晚些时候,史蒂夫给他带了一个午餐便当。身着一件米色外套和浅蓝色牛仔裤的哈特如约而至。尽管高出周围人许多,有着清爽面庞的他依旧那么帅气,那么平易近人。他问史蒂夫如果他之前是否尝试过构建一个60年代的超级英雄数据库。
67+
Later that afternoon, Steve was having a brown-bag lunch with him. Dressed in a beige blazer and light blue jeans, Hart arrived well in time. Despite being taller than most people and having a clean-shaven head, he seemed cool and approachable. He asked if Steve had checked out the previous attempt to build a superhero database in the sixties.
68+
那个下午的稍晚些时候,史蒂夫给他带了一个午餐便当。身着一件米色外套和浅蓝色牛仔裤的哈特如约而至。尽管高出周围人许多,有着清爽面庞的他依旧那么帅气,那么平易近人。他问史蒂夫他之前是否尝试过构建一个60年代的超级英雄数据库。
4069

70+
"Oh yes, the Sentinel project, right?" said Steve. "I did. The database seemed to be designed as an Entity-Attribute-Value model, something that I consider an anti-pattern. Perhaps they had very little idea about the attributes of a superhero those days." Hart almost winced at the last statement. In a slightly lowered voice, he said, "You are right, I didn't. Besides, they gave me only two days to design the whole thing. I believe there was literally a nuclear bomb ticking somewhere."
4171
”嗯,对的,是哨兵项目么?“,史蒂夫说道。”是我设计的。数据库看上去被设计成了一个条目-属性-值形式的模式,有些地方我考虑用反模式。可能,这些天他们有一些超级英雄属性的小想法。哈特几乎等不到听完最后一句,他压低嗓门道:“没错。是我的错。另外,他们只给了我两天来设计整个架构。他们这是在要我老命啊!”
4272

73+
Steve's mouth was wide open and his sandwich had frozen at its entrance. Hart smiled. "Certainly not my best work. Once it crossed about a billion entries, it took us days to run any kind of analysis on that damn database. SuperBook would zip through that in mere seconds, right?"
4374
听了这些,史蒂夫的嘴巴张的大大的,三明治也卡在口中。哈特微笑着道:“当然了,我还没有尽全力来做这件事。只要它成长为100万美元的单子,我们就可以多花点时间在这该死的数据库上了。SuperBook用它就能分分钟完事的,小史你说呢?”
4475

45-
史蒂夫微微点头称是。他从来没有想过在这么样的地方将会有上百万的超级英雄出现。
76+
Steve nodded weakly. He had never imagined that there would be around a billion superheroes in the first place.
77+
史蒂夫微微点头称是。他从来没有想过在这么样的地方将会有上百万的超级英雄出现。
4678

4779

4880
## 模型搜寻
49-
这是在SuperBook中确定模型的第一部分。通常对于一个早期试验,我们只表示了基本模型,以及在一个类表中的表单的基本关系:
50-
51-
图:略
81+
Here is a first cut at identifying the models in SuperBook. Typical to an early attempt, we have represented only the essential models and their relationships in the form of a class diagram:
82+
这是在SuperBook中确定模型的第一部分。通常对于一个早期试验,我们只表示了基本模型,以及在一个类表中的表单的基本关系:
5283

53-
让我们忘掉模型一会儿,来谈谈我们正在构建的对象的术语。每个用户都有一个账户。用户可以写多个回复或者多篇文章。**Like**可以同时关联到一个单独的用户或者文章。
84+
图:
85+
![2015-05-28 14 27 35](https://www.packtpub.com/graphics/9781783986644/graphics/6644_03_01.jpg)
5486

87+
Let's forget models for a moment and talk in terms of the objects we are modeling. Each user has a profile. A user can make several comments or several posts. A Like can be related to a single user/post combination.
88+
让我们忘掉模型一会儿,来谈谈我们正在构建的对象的术语。每个用户都有一个账户。用户可以写多个回复或者多篇文章。**Like**可以同时关联到一个单独的用户或者文章。
5589

90+
Drawing a class diagram of your models like this is recommended. Some attributes might be missing at this stage but you can detail them later. Once the entire project is represented in the diagram, it makes separating the apps easier.
5691
推荐你像这样画一个模型的类表。这个步骤的某些属性或许缺失了,不过你可以在之后补充细节。只要整个项目用图表表示出来,便可以轻松地分离app。
5792

93+
Here are some tips to create this representation:
5894
这是创建此表示的一些提示:
5995

60-
盒子表示条目,它将成为模型。
61-
名词通常作为条目的终止。
62-
箭头是双向的,表示Django中三种关系类型其中的一种:一对一,一对多(通过外键实现),和多对多。
63-
字段表明在基于条目-关系模型(ER-modle)的模型中定义了一对多关系。换句来说,星号是声明外键的地方。
96+
>Boxes represent entities, which become models.
97+
>盒子表示条目,它将成为模型。
98+
99+
>Nouns in your write-up typically end up as entities.
100+
>名词通常作为条目的终止。
101+
102+
>Arrows are bi-directional and represent one of the three types of relationships in Django: one-to-one, one-to-many (implemented with Foreign Keys), and many-to-many.
103+
>箭头是双向的,表示Django中三种关系类型其中的一种:一对一,一对多(通过外键实现),和多对多。
104+
105+
>The field denoting the one-to-many relationship is defined in the model on the Entity-relationship model (ER-model). In other words, the star is where the Foreign Key gets declared.
106+
>字段表明在基于条目-关系模型(ER-modle)的模型中定义了一对多关系。换句来说,星号是声明外键的地方。
64107
108+
The class diagram can be mapped into the following Django code (which will be spread across several apps):
65109
类图表可以映射到下面的Django代码中(它将遍及多个app):
66110

67111
```python
@@ -80,12 +124,15 @@
80124
post = models.ForeignKey(Post)
81125
```
82126

127+
Later, we will not reference the User directly but use the more general settings.AUTH_USER_MODEL instead.
83128
这之后,我们不会直接地引用*User*,而是使用更加普通的*settings.AUTH_USER_MODEL*来替代。
84129

85130

86131
### 分割model.py到多个文件
132+
Like most components of Django, a large models.py file can be split up into multiple files within a package. A package is implemented as a directory, which can contain multiple files, one of which must be a specially named file called __init__.py.
87133
就像多数的Django组件那样,一个大的model.py文件可以在一个包内分割为多个文件。**package**通过一个目录来实现,它包含多个文件,目录中的一个文件必须是一个称为`__init__.py`特殊文件。
88134

135+
All definitions that can be exposed at package level must be defined in __init__.py with global scope. For example, if we split models.py into individual classes, in corresponding files inside models subdirectory such as postable.py, post.py, and comment.py, then the __init__.py package will look like:
89136
所有可以在包级别中暴露的定义都必须在`__init__.py`里使用全局变量域定义。例如,如果我们分割model.py到独立的类,models子文件夹中的对应文件,比如,postable.py,post.py和comment.py, 之后`__init__.py`包会像这样:
90137

91138
```python
@@ -94,19 +141,25 @@
94141
from commnet import Comment
95142
```
96143

144+
Now you can import models.Post as before.
97145
现在你可以像之前那样导入models.Post了。
98146

147+
Any other code in the __init__.py package will be run when the package is imported. Hence, it is the ideal place for any package-level initialization code.
99148
`__init__.py`包中的任何其他代码都会在包运行时被导入。因此,它是一个任意级别包初始化代码的理想之地。
100149

101150
## 结构模式
151+
This section contains several design patterns that can help you design and structure your models.
102152
本节包含多个帮助你设计和构建模型的设计模式。
103153

104154
### 模式-规范化模型
155+
**Problem**: By design, model instances have duplicated data that cause data inconsistencies.
105156
**问题**:通过设计,模型实例的重复数据引起数据不一致。
106157

158+
**Solution**: Break down your models into smaller models through normalization. Connect these models with logical relationships between them.
107159
**解决方法**:通过规范化,分解模型到更小的模型。使用这些模型之间的逻辑关系来连接他们。
108160

109161
### 问题细节
162+
Imagine if someone designed our Post table (omitting certain columns) in the following way:
110163
想象一下,如果某人用下面的方法设计Post表(省略部分列):
111164

112165

@@ -845,4 +898,4 @@ from django.db.models import Q
845898
## 总结
846899
模型设计要正确地操作很困难。它依然是Django开发的基础。本章,我们学习了使用模型时多个常见模式。每个例子中,我们都见到了建议方案的作用,以及多种折衷方案。
847900

848-
这下一章,我们会用视图和URL配置来验证所遇到的常见设计模式。
901+
这下一章,我们会用视图和URL配置来验证所遇到的常见设计模式。

0 commit comments

Comments
 (0)