ID English原文 中文翻译 最近翻译记录 状态 操作
0#翻译 Chapter 5: Models 第5章 模型 翻译
3#翻译 In Chapter 3, we covered the fundamentals of building dynamic Web sites with Django: 在第三章,我们讲述了用 Django 建造网站的基本途径: 翻译
4#翻译 setting up views and URLconfs. 建立视图和 URLConf 。 翻译
5#翻译 As we explained, a view is responsible for doing <emphasis>some arbitrary logic</emphasis> , and then returning a response. 正如我们所阐述的,视图负责处理<emphasis>一些主观逻辑</emphasis>,然后返回响应结果。 翻译
6#翻译 In one of the examples, our arbitrary logic was to calculate the current date and time. 作为例子之一,我们的主观逻辑是要计算当前的日期和时间。 翻译
8#翻译 In modern Web applications, the arbitrary logic often involves interacting with a database. 在当代 Web 应用中,主观逻辑经常牵涉到与数据库的交互。 翻译
9#翻译 Behind the scenes, a <emphasis>database-driven Web site</emphasis> connects to a database server, retrieves some data out of it, and displays that data on a Web page. <emphasis>数据库驱动网站</emphasis> 在后台连接数据库服务器,从中取出一些数据,然后在 Web 页面用漂亮的格式展示这些数据。 翻译
10#翻译 The site might also provide ways for site visitors to populate the database on their own. 这个网站也可能会向访问者提供修改数据库数据的方法。 翻译
12#翻译 Many complex Web sites provide some combination of the two. 许多复杂的网站都提供了以上两个功能的某种结合。 翻译
13#翻译 Amazon.com, for instance, is a great example of a database-driven site. 例如 Amazon.com 就是一个数据库驱动站点的良好范例。 翻译
14#翻译 Each product page is essentially a query into Amazon's product database formatted as HTML, and when you post a customer review, it gets inserted into the database of reviews. 本质上,每个产品页面都是数据库中数据以 HTML格式进行的展现,而当你发表客户评论时,该评论被插入评论数据库中。 翻译
16#翻译 Django is well suited for making database-driven Web sites, because it comes with easy yet powerful tools for performing database queries using Python. 由于先天具备 Python 简单而强大的数据库查询执行方法,Django 非常适合开发数据库驱动网站。 翻译
17#翻译 This chapter explains that functionality: 本章深入介绍了该功能: 翻译
18#翻译 Django's database layer. Django 数据库层。 翻译
20#翻译 (Note: (注意: 翻译
21#翻译 While it's not strictly necessary to know basic relational database theory and SQL in order to use Django's database layer, its highly recommended. 尽管对 Django 数据库层的使用中并不特别强调这点,但是我们还是强烈建议您掌握一些数据库和 SQL 原理。 翻译
22#翻译 An introduction to those concepts is beyond the scope of this book, but keep reading even if you're a database newbie. 对这些概念的介绍超越了本书的范围,但就算你是数据库方面的菜鸟,我们也建议你继续阅读。 翻译
23#翻译 You'll probably be able to follow along and grasp concepts based on the context.) 你也许能够跟上进度,并在上下文学习过程中掌握一些概念。) 翻译
25#翻译 The Dumb Way to Do Database Queries in Views 在视图中进行数据库查询的笨方法 翻译
27#翻译 Just as Chapter 3 detailed a dumb way to produce output within a view (by hard-coding the text directly within the view), there's a dumb way to retrieve data from a database in a view. 正如第三章详细介绍的那个在视图中输出 HTML 的笨方法(通过在视图里对文本直接硬编码HTML),在视图中也有笨方法可以从数据库中获取数据。 翻译
28#翻译 It's simple: 很简单: 翻译
29#翻译 just use any existing Python library to execute an SQL query and do something with the results. 用现有的任何 Python 类库执行一条 SQL 查询并对结果进行一些处理。 翻译
31#翻译 In this example view, we use the <literal>MySQLdb</literal> library (available via <reference name="http://www.djangoproject.com/r/python-mysql/" refuri="http://www.djangoproject.com/r/python-mysql/">http://www.djangoproject.com/r/python-mysql/</reference>) to connect to a MySQL database, retrieve some records, and feed them to a template for display as a Web page: 在本例的视图中,我们使用了 <literal>MySQLdb</literal> 类库(可以从 <reference name="http://www.djangoproject.com/r/python-mysql/" refuri="http://www.djangoproject.com/r/python-mysql/">http://www.djangoproject.com/r/python-mysql/</reference> 获得)来连接 MySQL 数据库,取回一些记录,将它们提供给模板以显示一个网页: 翻译
34#翻译 This approach works, but some problems should jump out at you immediately: 这个方法可用,但很快一些问题将出现在你面前: 翻译
36#翻译 We're hard-coding the database connection parameters. 我们将数据库连接参数硬行编码于代码之中。 翻译
37#翻译 Ideally, these parameters would be stored in the Django configuration. 理想情况下,这些参数应当保存在 Django 配置中。 翻译
39#翻译 We're having to write a fair bit of boilerplate code: 我们不得不重复同样的代码: 翻译
40#翻译 creating a connection, creating a cursor, executing a statement, and closing the connection. 创建数据库连接、创建数据库游标、执行某个语句、然后关闭数据库。 翻译
41#翻译 Ideally, all we'd have to do is specify which results we wanted. 理想情况下,我们所需要应该只是指定所需的结果。 翻译
43#翻译 It ties us to MySQL. 它把我们栓死在 MySQL 之上。 翻译
44#翻译 If, down the road, we switch from MySQL to PostgreSQL, we'll have to use a different database adapter (e.g., <literal>psycopg</literal> rather than <literal>MySQLdb</literal> ), alter the connection parameters, and depending on the nature of the SQL statement possibly rewrite the SQL. 如果过段时间,我们要从 MySQL 换到 PostgreSQL,就不得不使用不同的数据库适配器(例如 <literal>psycopg</literal> 而不是 <literal>MySQLdb</literal> ),改变连接参数,根据 SQL 语句的类型可能还要修改SQL 。 翻译
45#翻译 Ideally, the database server we're using would be abstracted, so that a database server change could be made in a single place. 理想情况下,应对所使用的数据库服务器进行抽象,这样一来只在一处修改即可变换数据库服务器。 翻译
46#翻译 (This feature is particularly relevant if you're building an open-source Django application that you want to be used by as many people as possible.) (如果你正在建立一个开源的Django应用程序来尽可能让更多人使用的话,这个特性是非常适当的。) 翻译
48#翻译 As you might expect, Django's database layer aims to solve these problems. 正如你所期待的,Django数据库层正是致力于解决这些问题。 翻译
49#翻译 Here's a sneak preview of how the previous view can be rewritten using Django's database API: 以下提前揭示了如何使用 Django 数据库 API 重写之前那个视图。 翻译
52#翻译 We'll explain this code a little later in the chapter. 我们将在本章稍后的地方解释这段代码。 翻译
53#翻译 For now, just get a feel for how it looks. 目前而言,仅需对它有个大致的认识。 翻译
55#翻译 The MTV (or MVC) Development Pattern MTV 开发模式 翻译
57#翻译 Before we delve into any more code, let's take a moment to consider the overall design of a database-driven Django Web application. 在钻研更多代码之前,让我们先花点时间考虑下 Django 数据驱动 Web 应用的总体设计。 翻译
59#翻译 As we mentioned in previous chapters, Django is designed to encourage loose coupling and strict separation between pieces of an application. 我们在前面章节提到过,Django 的设计鼓励松耦合及对应用程序中不同部分的严格分割。 翻译
60#翻译 If you follow this philosophy, it's easy to make changes to one particular piece of the application without affecting the other pieces. 遵循这个理念的话,要想修改应用的某部分而不影响其它部分就比较容易了。 翻译
61#翻译 In view functions, for instance, we discussed the importance of separating the business logic from the presentation logic by using a template system. 在视图函数中,我们已经讨论了通过模板系统把业务逻辑和表现逻辑分隔开的重要性。 翻译
62#翻译 With the database layer, we're applying that same philosophy to data access logic. 在数据库层中,我们对数据访问逻辑也应用了同样的理念。 翻译
64#翻译 Those three pieces together data access logic, business logic, and presentation logic comprise a concept that's sometimes called the <emphasis>Model-View-Controller</emphasis> (MVC) pattern of software architecture. 把数据存取逻辑、业务逻辑和表现逻辑组合在一起的概念有时被称为软件架构的 <emphasis>Model-View-Controller</emphasis> (MVC)模式。 翻译
65#翻译 In this pattern, Model refers to the data access layer, View refers to the part of the system that selects what to display and how to display it, and Controller refers to the part of the system that decides which view to use, depending on user input, accessing the model as needed. 在这个模式中, Model 代表数据存取层,View 代表的是系统中选择显示什么和怎么显示的部分,Controller 指的是系统中根据用户输入并视需要访问模型,以决定使用哪个视图的那部分。 翻译
67#翻译 Why the Acronym? 为什么用缩写? 翻译
69#翻译 The goal of explicitly defining patterns such as MVC is mostly to streamline communication among developers. 像 MVC 这样的明确定义模式的主要用于改善开发人员之间的沟通。 翻译
70#翻译 Instead of having to tell your coworkers, Let's make an abstraction of the data access, then let's have a separate layer that handles data display, and let's put a layer in the middle that regulates this, you can take advantage of a shared vocabulary and say, Let's use the MVC pattern here. 比起告诉同事,“让我们采用抽象的数据存取方式,然后单独划分一层来显示数据,并且在中间加上一个控制它的层”,一个通用的说法会让你收益,你只需要说:“我们在这里使用MVC模式吧。”。 翻译
72#翻译 Django follows this MVC pattern closely enough that it can be called an MVC framework. Django 紧紧地遵循这种 MVC 模式,可以称得上是一种 MVC 框架。 翻译
73#翻译 Here's roughly how the M, V, and C break down in Django: 以下是 Django 中 M、V 和 C 各自的含义: 翻译
75#翻译 <emphasis>M</emphasis> , the data-access portion, is handled by Django's database layer, which is described in this chapter. <emphasis>M</emphasis> ,数据存取部分,由django数据库层处理,本章要讲述的内容。 翻译
77#翻译 <emphasis>V</emphasis> , the portion that selects which data to display and how to display it, is handled by views and templates. <emphasis>V</emphasis> ,选择显示哪些数据要显示以及怎样显示的部分,由视图和模板处理。 翻译
79#翻译 <emphasis>C</emphasis> , the portion that delegates to a view depending on user input, is handled by the framework itself by following your URLconf and calling the appropriate Python function for the given URL. <emphasis>C</emphasis> ,根据用户输入委派视图的部分,由 Django 框架根据 URLconf 设置,对给定 URL 调用适当的 Python 函数。 翻译
81#翻译 Because the C is handled by the framework itself and most of the excitement in Django happens in models, templates and views, Django has been referred to as an <emphasis>MTV framework</emphasis> . In the MTV development pattern, 由于 C 由框架自行处理,而 Django 里更关注的是模型(Model)、模板(Template)和视图(Views),Django 也被称为 <emphasis>MTV 框架</emphasis> 。在 MTV 开发模式中: 翻译
83#翻译 <emphasis>M</emphasis> stands for Model, the data access layer. <emphasis>M</emphasis> 代表模型(Model),即数据存取层。 翻译
84#翻译 This layer contains anything and everything about the data: 该层处理与数据相关的所有事务: 翻译
85#翻译 how to access it, how to validate it, which behaviors it has, and the relationships between the data. 如何存取、如何验证有效性、包含哪些行为以及数据之间的关系等。 翻译
87#翻译 <emphasis>T</emphasis> stands for Template, the presentation layer. <emphasis>T</emphasis> 代表模板(Template),即表现层。 翻译
88#翻译 This layer contains presentation-related decisions: 该层处理与表现相关的决定: 翻译
89#翻译 how something should be displayed on a Web page or other type of document. 如何在页面或其他类型文档中进行显示。 翻译
91#翻译 <emphasis>V</emphasis> stands for View, the business logic layer. <emphasis>V</emphasis> 代表视图(View),即业务逻辑层。 翻译
92#翻译 This layer contains the logic that access the model and defers to the appropriate template(s). 该层包含存取模型及调取恰当模板的相关逻辑。 翻译
93#翻译 You can think of it as the bridge between models and templates. 你可以把它看作模型与模板之间的桥梁。 翻译
95#翻译 If you're familiar with other MVC Web-development frameworks, such as Ruby on Rails, you may consider Django views to be the controllers and Django templates to be the views. 如果你熟悉其它的 MVC Web开发框架,比方说 Ruby on Rails,你可能会认为 Django 视图是控制器,而 Django 模板是视图。 翻译
96#翻译 This is an unfortunate confusion brought about by differing interpretations of MVC. 很不幸,这是对 MVC 不同诠释所引起的错误认识。 翻译
97#翻译 In Django's interpretation of MVC, the view describes the data that gets presented to the user; it's not necessarily just <emphasis>how</emphasis> the data looks, but <emphasis>which</emphasis> data is presented. 在 Django 对 MVC 的诠释中,视图用来描述要展现给用户的数据;不是数据 <emphasis>如何</emphasis>展现 ,而且展现 <emphasis>哪些</emphasis> 数据。 翻译
98#翻译 In contrast, Ruby on Rails and similar frameworks suggest that the controller's job includes deciding which data gets presented to the user, whereas the view is strictly <emphasis>how</emphasis> the data looks, not <emphasis>which</emphasis> data is presented. 相比之下,Ruby on Rails 及一些同类框架提倡控制器负责决定向用户展现哪些数据,而视图则仅决定 <emphasis>如何</emphasis> 展现数据,而不是展现 <emphasis>哪些</emphasis> 数据。 翻译
100#翻译 Neither interpretation is more correct than the other. 两种诠释中没有哪个更加正确一些。 翻译
101#翻译 The important thing is to understand the underlying concepts. 重要的是要理解底层概念。 翻译
103#翻译 Configuring the Database 数据库配置 翻译
105#翻译 With all of that philosophy in mind, let's start exploring Djangos database layer. 记住这些理念之后,让我们来开始 Django 数据库层的探索。 翻译
106#翻译 First, we need to take care of some initial configuration; we need to tell Django which database server to use and how to connect to it. 首先,我们需要做些初始配置;我们需要告诉Django使用什么数据库以及如何连接数据库。 翻译
108#翻译 We'll assume youve set up a database server, activated it, and created a database within it (e.g., using a <literal>CREATE DATABASE</literal> statement). 我们假定你已经完成了数据库服务器的安装和激活,并且已经在其中创建了数据库(例如,用 <literal>CREATE DATABASE</literal> 语句)。 翻译
109#翻译 If you're using SQLite, no such setup is required, because SQLite uses standalone files on the filesystem to store its data. 如果你使用SQLite,不需要这步安装,因为SQLite使用文件系统上的独立文件来存储数据。 翻译
111#翻译 As with <literal>TEMPLATE_DIRS</literal> in the previous chapter, database configuration lives in the Django settings file, called <literal>settings.py</literal> by default. 象前面章节提到的 <literal>TEMPLATE_DIRS</literal> 一样,数据库配置也是在Django的配置文件里,缺省 是 <literal>settings.py</literal> 。 翻译
112#翻译 Edit that file and look for the database settings: 打开这个文件并查找数据库配置: 翻译
115#翻译 Here's a rundown of each setting. 配置纲要如下。 翻译
117#翻译 <literal>DATABASE_ENGINE</literal> tells Django which database engine to use. <literal>DATABASE_ENGINE</literal> 告诉Django使用哪个数据库引擎。 翻译
118#翻译 If you're using a database with Django, <literal>DATABASE_ENGINE</literal> must be set to one of the strings shown in Table 5-1. 如果你在 Django 中使用数据库, <literal>DATABASE_ENGINE</literal> 必须是 Table 5-1 中所列出的值。 翻译
121#翻译 Table 5-1. 表 5-1. 翻译
122#翻译 Database Engine Settings 数据库引擎设置 翻译
124#翻译 Setting 设置 翻译
126#翻译 Database 数据库 翻译
128#翻译 Required Adapter 所需适配器 翻译
130#翻译 <literal>postgresql</literal> <literal> postgresql</literal> 翻译
132#翻译 PostgreSQL PostgreSQL 翻译
134#翻译 <literal>psycopg</literal> version 1.x, <reference name="http://www.djangoproject.com/r/python-pgsql/1/" refuri="http://www.djangoproject.com/r/python-pgsql/1/">http://www.djangoproject.com/r/python-pgsql/1/</reference>. <literal>psycopg</literal> 1.x版, <reference name="http://www.djangoproject.com/r/python-pgsql/1/" refuri="http://www.djangoproject.com/r/python-pgsql/1/">http://www.djangoproject.com/r/python-pgsql/1/</reference>。 翻译
136#翻译 <literal>postgresql_psycopg2</literal> <literal>postgresql_psycopg2</literal> 翻译
138#翻译 PostgreSQL PostgreSQL 翻译
140#翻译 <literal>psycopg</literal> version 2.x, <reference name="http://www.djangoproject.com/r/python-pgsql/" refuri="http://www.djangoproject.com/r/python-pgsql/">http://www.djangoproject.com/r/python-pgsql/</reference>. <literal>psycopg</literal> 2.x版, <reference name="http://www.djangoproject.com/r/python-pgsql/" refuri="http://www.djangoproject.com/r/python-pgsql/">http://www.djangoproject.com/r/python-pgsql/</reference>。 翻译
142#翻译 <literal>mysql</literal> <literal>mysql</literal> 翻译
144#翻译 MySQL MySQL 翻译
146#翻译 <literal>MySQLdb</literal> , <reference name="http://www.djangoproject.com/r/python-mysql/" refuri="http://www.djangoproject.com/r/python-mysql/">http://www.djangoproject.com/r/python-mysql/</reference>. <literal>MySQLdb</literal> , <reference name="http://www.djangoproject.com/r/python-mysql/" refuri="http://www.djangoproject.com/r/python-mysql/">http://www.djangoproject.com/r/python-mysql/</reference>. 翻译
148#翻译 <literal>sqlite3</literal> <literal>sqlite3</literal> 翻译
150#翻译 SQLite SQLite 翻译
152#翻译 No adapter needed if using Python 2.5+. 如果使用Python 2.5+则不需要适配器。 翻译
153#翻译 Otherwise, <literal>pysqlite</literal> , <reference name="http://www.djangoproject.com/r/python-sqlite/" refuri="http://www.djangoproject.com/r/python-sqlite/">http://www.djangoproject.com/r/python-sqlite/</reference>. 否则就使用 <literal>pysqlite</literal> , <reference name="http://www.djangoproject.com/r/python-sqlite/" refuri="http://www.djangoproject.com/r/python-sqlite/">http://www.djangoproject.com/r/python-sqlite/</reference>。 翻译
155#翻译 <literal>oracle</literal> <literal>oracle</literal> 翻译
157#翻译 Oracle Oracle 翻译
159#翻译 <literal>cx_Oracle</literal> , <reference name="http://www.djangoproject.com/r/python-oracle/" refuri="http://www.djangoproject.com/r/python-oracle/">http://www.djangoproject.com/r/python-oracle/</reference>. <literal>cx_Oracle</literal> , <reference name="http://www.djangoproject.com/r/python-oracle/" refuri="http://www.djangoproject.com/r/python-oracle/">http://www.djangoproject.com/r/python-oracle/</reference>. 翻译
161#翻译 Note that for whichever database back-end you use, you'll need to download and install the appropriate database adapter. 要注意的是无论选择使用哪个数据库服务器,都必须下载和安装对应的数据库适配器。 翻译
162#翻译 Each one is available for free on the Web; just follow the links in the Required Adapter column in Table 5-1. 访问表 5-1 中“所需适配器”一栏中的链接,可通过互联网免费获取这些适配器。 翻译
163#翻译 If you're on Linux, your distributions package-management system might offer convenient packages. 如果你使用Linux,你的发布包管理系统会提供合适的包。 翻译
164#翻译 (Look for packages called <literal>python-postgresql</literal> or <literal>python-psycopg</literal> , for example.) 比如说查找<literal> python-postgresql</literal> 或者<literal> python-psycopg</literal> 的软件包。 翻译
166#翻译 Example: 配置示例: 翻译
169#翻译 <literal>DATABASE_NAME</literal> tells Django the name of your database. <literal>DATABASE_NAME</literal> 将数据库名称告知 Django 。 翻译
170#翻译 For example: 例如: 翻译
173#翻译 If you're using SQLite, specify the full filesystem path to the database file on your filesystem. 如果使用 SQLite,请对数据库文件指定完整的文件系统路径。 翻译
174#翻译 For example: 例如: 翻译
177#翻译 As for where you put that SQLite database, we're using the <literal>/home/django</literal> directory in this example, but you should pick a directory that works best for you. 在这个例子中,我们将SQLite数据库放在/home/django目录下,你可以任意选用最合适你的目录。 翻译
179#翻译 <literal>DATABASE_USER</literal> tells Django which username to use when connecting to your database. <literal>DATABASE_USER</literal> 告诉 Django 用哪个用户连接数据库。 翻译
180#翻译 For example: 例如: 翻译
181#翻译 If you're using SQLite, leave this blank. 如果用SQLite,空白即可。 翻译
183#翻译 <literal>DATABASE_PASSWORD</literal> tells Django which password to use when connecting to your database. <literal>DATABASE_PASSWORD</literal> 告诉Django连接用户的密码。 翻译
184#翻译 If you're using SQLite or have an empty password, leave this blank. SQLite 用空密码即可。 翻译
186#翻译 <literal>DATABASE_HOST</literal> tells Django which host to use when connecting to your database. <literal>DATABASE_HOST</literal> 告诉 Django 连接哪一台主机的数据库服务器。 翻译
187#翻译 If your database is on the same computer as your Django installation (i.e., localhost), leave this blank. 如果数据库与 Django 安装于同一台计算机(即本机),可将此项保留空白。 翻译
188#翻译 If you're using SQLite, leave this blank. 如果你使用SQLite,此项留空。 翻译
190#翻译 MySQL is a special case here. 此处的 MySQL 是一个特例。 翻译
191#翻译 If this value starts with a forward slash (<literal>'/'</literal> ) and you're using MySQL, MySQL will connect via a Unix socket to the specified socket, for example: 如果使用的是 MySQL 且该项设置值由斜杠( <literal>'/'</literal> )开头,MySQL 将通过 Unix socket 来连接指定的套接字,例如: 翻译
194#翻译 Once you've entered those settings and saved <literal>settings.py</literal> , it's a good idea to test your configuration. 一旦在输入了那些设置并保存之后应当测试一下你的配置。 翻译
195#翻译 To do this, run <literal>python manage.py shell</literal> as in the last chapter, from within the <literal>mysite</literal> project directory. 我们可以在<literal> mysite</literal> 项目目录下执行上章所提到的<literal> python manage.py shell</literal> 来进行测试。 翻译
196#翻译 (As we pointed out last chapter <literal>manage.py shell</literal> is a way to run the Python interpreter with the correct Django settings activated. (我们上一章提到过在,<literal> manager.py shell</literal> 命令是以正确Django配置启用Python交互解释器的一种方法。 翻译
197#翻译 This is necessary in our case, because Django needs to know which settings file to use in order to get your database connection information.) 这个方法在这里是很有必要的,因为Django需要知道加载哪个配置文件来获取数据库连接信息。) 翻译
199#翻译 In the shell, type these commands to test your database configuration: 输入下面这些命令来测试你的数据库配置: 翻译
202#翻译 If nothing happens, then your database is configured properly. 如果没有显示什么错误信息,那么你的数据库配置是正确的。 翻译
203#翻译 Otherwise, check the error message for clues about what's wrong. 否则,你就得 查看错误信息来纠正错误。 翻译
204#翻译 Table 5-2 shows some common errors. 表 5-2 是一些常见错误。 翻译
207#翻译 Table 5-2. 表 5-2. 翻译
208#翻译 Database Configuration Error Messages 数据库配置错误信息 翻译
210#翻译 Error Message 错误信息 翻译
212#翻译 Solution 解决方法 翻译
214#翻译 You haven't set the DATABASE_ENGINE setting yet. You haven't set the DATABASE_ENGINE setting yet. 翻译
216#翻译 Set the <literal>DATABASE_ENGINE</literal> setting to something other than an empty string. 不要以空字符串配置<literal> DATABASE_ENGINE</literal> 的值。 翻译
217#翻译 Valid values are in Table 5-1. 表格 5-1 列出可用的值。 翻译
219#翻译 Environment variable DJANGO_SETTINGS_MODULE is undefined. Environment variable DJANGO_SETTINGS_MODULE is undefined. 翻译
221#翻译 Run the command <literal>python manage.py shell</literal> rather than <literal>python</literal> . 使用<literal> python manager.py shell</literal> 命令启动交互解释器,不要以<literal> python</literal> 命令直接启动交互解释器。 翻译
223#翻译 Error loading _____ module: Error loading _____ module: 翻译
224#翻译 No module named _____. No module named _____. 翻译
226#翻译 You haven't installed the appropriate database-specific adapter (e.g., <literal>psycopg</literal> or <literal>MySQLdb</literal> ). Adapters are <emphasis>not</emphasis> bundled with Django, so it's your responsibility to download and install them on your own. 未安装合适的数据库适配器 (例如, <literal>psycopg</literal> 或 <literal>MySQLdb</literal> )。Django并不自带适配器,所以你得自己下载安装。 翻译
228#翻译 _____ isn't an available database backend. _____ isn't an available database backend. 翻译
230#翻译 Set your <literal>DATABASE_ENGINE</literal> setting to one of the valid engine settings described previously. 把<literal>DATABASE_ENGINE</literal> 配置成前面提到的合法的数据库引擎。 翻译
231#翻译 Perhaps you made a typo? 也许是拼写错误? 翻译
233#翻译 database _____ does not exist database _____ does not exist 翻译
235#翻译 Change the <literal>DATABASE_NAME</literal> setting to point to a database that exists, or execute the appropriate <literal>CREATE DATABASE</literal> statement in order to create it. 设置<literal> DATABASE_NAME</literal> 指向存在的数据库,或者先在数据库客户端中执行合适的<literal> CREATE DATABASE</literal> 语句创建数据库。 翻译
237#翻译 role _____ does not exist role _____ does not exist 翻译
239#翻译 Change the <literal>DATABASE_USER</literal> setting to point to a user that exists, or create the user in your database. 设置<literal> DATABASE_USER</literal> 指向存在的用户,或者先在数据库客户端中执创建用户。 翻译
241#翻译 could not connect to server could not connect to server 翻译
243#翻译 Make sure <literal>DATABASE_HOST</literal> and <literal>DATABASE_PORT</literal> are set correctly, and make sure the database server is running. 查看DATABASE_HOST和DATABASE_PORT是否已正确配置,并确认数据库服务器是否已正常运行。 翻译
245#翻译 Your First App 第一个应用程序 翻译
247#翻译 Now that you've verified the connection is working, it's time to create a <emphasis>Django app</emphasis> a bundle of Django code, including models and views, that lives together in a single Python package and represents a full Django application. 你现在已经确认数据库连接正常工作了,让我们来创建一个 <emphasis>Django app</emphasis>-一个包含模型,视图和Django代码,并且形式为独立Python包的完整Django应用。 翻译
249#翻译 It's worth explaining the terminology here, because this tends to trip up beginners. 在这里要先解释一些术语,初学者可能会混淆它们。 翻译
250#翻译 We'd already created a <emphasis>project</emphasis> , in Chapter 2, so what's the difference between a <emphasis>project</emphasis> and an <emphasis>app</emphasis> ? The difference is that of configuration vs. 在第二章我们已经创建了 <emphasis>project</emphasis> , 那么 <emphasis>project</emphasis> 和 <emphasis>app</emphasis> 之间到底有什么不同呢?它们的区别就是一个是配置另一个是 翻译
251#翻译 code: 代码: 翻译
253#翻译 A project is an instance of a certain set of Django apps, plus the configuration for those apps. 一个project包含很多个Django app以及对它们的配置。 翻译
255#翻译 Technically, the only requirement of a project is that it supplies a settings file, which defines the database connection information, the list of installed apps, the <literal>TEMPLATE_DIRS</literal> , and so forth. 技术上,project的作用是提供配置文件,比方说哪里定义数据库连接信息, 安装的app列表, <literal>TEMPLATE_DIRS</literal> ,等等。 翻译
257#翻译 An app is a portable set of Django functionality, usually including models and views, that lives together in a single Python package. 一个app是一套Django功能的集合,通常包括模型和视图,按Python的包结构的方式存在。 翻译
259#翻译 For example, Django comes with a number of apps, such as a commenting system and an automatic admin interface. 例如,Django本身内建有一些app,例如注释系统和自动管理界面。 翻译
260#翻译 A key thing to note about these apps is that they're portable and reusable across multiple projects. app的一个关键点是它们是很容易移植到其他project和被多个project复用。 翻译
262#翻译 There are very few hard-and-fast rules about how you fit your Django code into this scheme. 对于如何架构Django代码并没有快速成套的规则。 翻译
263#翻译 If you're building a simple Web site, you may use only a single app. 如果你只是建造一个简单的Web站点,那么可能你只需要一个app就可以了; 翻译
264#翻译 If you're building a complex Web site with several unrelated pieces such as an e-commerce system and a message board, you'll probably want to split those into separate apps so that you'll be able to reuse them individually in the future. 但如果是一个包含许多不相关的模块的复杂的网站,例如电子商务和社区之类的站点,那么你可能需要把这些模块划分成不同的app,以便以后复用。 翻译
266#翻译 Indeed, you don't necessarily need to create apps at all, as evidenced by the example view functions we've created so far in this book. 不错,你可以不用创建app,这一点应经被我们之前编写的视图函数的例子证明了 。 翻译
267#翻译 In those cases, we simply created a file called <literal>views.py</literal> , filled it with view functions, and pointed our URLconf at those functions. 在那些例子中,我们只是简单的创建了一个称为<literal>views.py</literal>的文件,编写了一些函数并在URLconf中设置了各个函数的映射。 翻译
268#翻译 No apps were needed. 这些情况都不需要使用apps。 翻译
270#翻译 However, there's one requirement regarding the app convention: 但是,系统对app有一个约定: 翻译
271#翻译 if you're using Djangos database layer (models), you must create a Django app. 如果你使用了Django的数据库层(模型),你 必须创建一个Django app。 翻译
272#翻译 Models must live within apps. 模型必须存放在apps中。 翻译
273#翻译 Thus, in order to start writing our models, we'll need to create a new app. 因此,为了开始建造 我们的模型,我们必须创建一个新的app。 翻译
275#翻译 Within the <literal>mysite</literal> project directory, type this command to create a <literal>books</literal> app: 在<literal> mysite</literal> 项目文件下输入下面的命令来创建<literal> books</literal> app: 翻译
278#翻译 This command does not produce any output, but it does create a <literal>books</literal> directory within the <literal>mysite</literal> directory. 这个命令并没有输出什么,它只在 <literal>mysite</literal> 的目录里创建了一个 <literal>books</literal> 目录。 翻译
279#翻译 Let's look at the contents of that directory: 让我们来看看这个目录的内容: 翻译
282#翻译 These files will contain the models and views for this app. 这个目录包含了这个app的模型和视图。 翻译
284#翻译 Have a look at <literal>models.py</literal> and <literal>views.py</literal> in your favorite text editor. 使用你最喜欢的文本编辑器查看一下 <literal>models.py</literal> 和 <literal>views.py</literal> 文件的内容。 翻译
285#翻译 Both files are empty, except for comments and an import in <literal>models.py</literal> . This is the blank slate for your Django app. 它们都是空的,除了 <literal>models.py</literal> 里有一个 import。这就是你Django app的基础。 翻译
287#翻译 Defining Models in Python 在Python代码里定义模型 翻译
289#翻译 As we discussed earlier in this chapter, the M in MTV stands for Model. 我们早些时候谈到。MTV里的M代表模型。 翻译
290#翻译 A Django model is a description of the data in your database, represented as Python code. Django模型是用Python代码形式表述的数据在数据库中的定义。 翻译
291#翻译 It's your data layout the equivalent of your SQL <literal>CREATE TABLE</literal> statements except it's in Python instead of SQL, and it includes more than just database column definitions. 对数据层来说它等同于 CREATE TABLE 语句,只不过执行的是Python代码而不是 SQL,而且还包含了比数据库字段定义更多的含义。 翻译
292#翻译 Django uses a model to execute SQL code behind the scenes and return convenient Python data structures representing the rows in your database tables. Django用模型在后台执行SQL代码并把结果用Python的数据结构来描述。 翻译
293#翻译 Django also uses models to represent higher-level concepts that SQL can't necessarily handle. Django也使用模型来呈现SQL无法处理的高级概念。 翻译
295#翻译 If you're familiar with databases, your immediate thought might be, Isn't it redundant to define data models in Python instead of in SQL? 如果你对数据库很熟悉,你可能马上就会想到,用Python <emphasis>和</emphasis> SQL来定义数据模型是不是有点多余? 翻译
296#翻译 Django works the way it does for several reasons: Django这样做是有下面几个原因的: 翻译
298#翻译 Introspection requires overhead and is imperfect. 自省(运行时自动识别数据库)会导致过载和有数据完整性问题。 翻译
299#翻译 In order to provide convenient data-access APIs, Django needs to know the database layout <emphasis>somehow</emphasis> , and there are two ways of accomplishing this. 为了提供方便的数据访问API, Django需要以 <emphasis>某种方式</emphasis> 知道数据库层内部信息,有两种实现方式。 翻译
300#翻译 The first way would be to explicitly describe the data in Python, and the second way would be to introspect the database at runtime to determine the data models. 第一种方式是用Python明确地定义数据模型,第二种方式是通过自省来自动侦测识别数据模型。 翻译
302#翻译 This second way seems cleaner, because the metadata about your tables lives in only one place, but it introduces a few problems. 第二种方式看起来更清晰,因为数据表信息只存放在一个地方-数据库里,但是会带来一些问题。 翻译
303#翻译 First, introspecting a database at runtime obviously requires overhead. 首先,运行时扫描数据库会带来严重的系统过载。 翻译
304#翻译 If the framework had to introspect the database each time it processed a request, or even only when the Web server was initialized, this would incur an unacceptable level of overhead. 如果每个请求都要扫描数据库的表结构,或者即便是 服务启动时做一次都是会带来不能接受的系统过载。 翻译
305#翻译 (While some believe that level of overhead is acceptable, Django's developers aim to trim as much framework overhead as possible.) Second, some databases, notably older versions of MySQL, do not store sufficient metadata for accurate and complete introspection. (有人认为这个程度的系统过载是可以接受的,而Django开发者的目标是尽可能地降低框架的系统过载)。第二,某些数据库,尤其是老版本的MySQL,并未完整存储那些精确的自省元数据。 翻译
307#翻译 Writing Python is fun, and keeping everything in Python limits the number of times your brain has to do a context switch. 编写Python代码是非常有趣的,保持用Python的方式思考会避免你的大脑在不同领域来回切换。 翻译
308#翻译 It helps productivity if you keep yourself in a single programming environment/mentality for as long as possible. 尽可能的保持在单一的编程环境/思想状态下可以帮助你提高生产率。 翻译
309#翻译 Having to write SQL, then Python, and then SQL again is disruptive. 不得不去重复写SQL,再写Python代码,再写SQL,...,会让你头都要裂了。 翻译
311#翻译 Having data models stored as code rather than in your database makes it easier to keep your models under version control. 把数据模型用代码的方式表述来让你可以容易对它们进行版本控制。 翻译
312#翻译 This way, you can easily keep track of changes to your data layouts. 这样,你可以很容易了解数据层 的变动情况。 翻译
314#翻译 SQL allows for only a certain level of metadata about a data layout. SQL只能描述特定类型的数据字段。 翻译
315#翻译 Most database systems, for example, do not provide a specialized data type for representing email addresses or URLs. 例如,大多数数据库都没有专用的字段类型来描述Email地址、URL。 翻译
316#翻译 Django models do. 而用Django的模型可以做到这一点。 翻译
317#翻译 The advantage of higher-level data types is higher productivity and more reusable code. 好处就是高级的数据类型带来更高的效率和更好的代码复用。 翻译
319#翻译 SQL is inconsistent across database platforms. SQL还有在不同数据库平台的兼容性问题。 翻译
320#翻译 If you're distributing a Web application, for example, it's much more pragmatic to distribute a Python module that describes your data layout than separate sets of <literal>CREATE TABLE</literal> statements for MySQL, PostgreSQL, and SQLite. 发布Web应用的时候,使用Python模块描述数据库结构信息可以避免为MySQL, PostgreSQL, and SQLite编写不同的<literal>CREATE TABLE</literal>。 翻译
322#翻译 A drawback of this approach, however, is that it's possible for the Python code to get out of sync with what's actually in the database. 当然,这个方法也有一个缺点,就是Python代码和数据库表的同步问题。 翻译
323#翻译 If you make changes to a Django model, you'll need to make the same changes inside your database to keep your database consistent with the model. 如果你修改了一个Django模型, 你要自己来修改数据库来保证和模型同步。 翻译
324#翻译 We'll discuss some strategies for handling this problem later in this chapter. 我们将在稍后讲解解决这个问题的几种策略。 翻译
326#翻译 Finally, we should note that Django includes a utility that can generate models by introspecting an existing database. 最后,我们要提醒你Django提供了实用工具来从现有的数据库表中自动扫描生成模型。 翻译
327#翻译 This is useful for quickly getting up and running with legacy data. 这对已有的数据库来说是非常快捷有用的。 翻译
328#翻译 We'll cover this in Chapter 18. 我们将在第18章中对此进行讨论。 翻译
330#翻译 Your First Model 第一个模型 翻译
332#翻译 As an ongoing example in this chapter and the next chapter, we'll focus on a basic book/author/publisher data layout. 在本章和后续章节里,我们把注意力放在一个基本的 书籍/作者/出版商 数据库结构上。 翻译
333#翻译 We use this as our example because the conceptual relationships between books, authors, and publishers are well known, and this is a common data layout used in introductory SQL textbooks. 我们这样做是因为 这是一个众所周知的例子,很多SQL有关的书籍也常用这个举例。 翻译
334#翻译 You're also reading a book that was written by authors and produced by a publisher! 你现在看的这本书也是由作者 创作再由出版商出版的哦! 翻译
336#翻译 We'll suppose the following concepts, fields, and relationships: 我们来假定下面的这些概念、字段和关系: 翻译
338#翻译 An author has a first name, a last name and an email address. 一个作者有姓,有名及email地址。 翻译
340#翻译 A publisher has a name, a street address, a city, a state/province, a country, and a Web site. 出版商有名称,地址,所在城市、省,国家,网站。 翻译
342#翻译 A book has a title and a publication date. 书籍有书名和出版日期。 翻译
343#翻译 It also has one or more authors (a many-to-many relationship with authors) and a single publisher (a one-to-many relationship aka foreign key to publishers). 它有一个或多个作者(和作者是多对多的关联关系[many-to-many]), 只有一个出版商(和出版商是一对多的关联关系[one-to-many],也被称作外键[foreign key]) 翻译
345#翻译 The first step in using this database layout with Django is to express it as Python code. 第一步是用Python代码来描述它们。 翻译
346#翻译 In the <literal>models.py</literal> file that was created by the <literal>startapp</literal> command, enter the following: 打开由<literal> startapp</literal> 命令创建的<literal>models.py</literal> 并输入下面的内容: 翻译
349#翻译 Let's quickly examine this code to cover the basics. 让我们来快速讲解一下这些代码的含义。 翻译
350#翻译 The first thing to notice is that each model is represented by a Python class that is a subclass of <literal>django.db.models.Model</literal> . The parent class, <literal>Model</literal> , contains all the machinery necessary to make these objects capable of interacting with a database and that leaves our models responsible solely for defining their fields, in a nice and compact syntax. 首先要注意的事是每个数据模型都是 <literal>django.db.models.Model</literal> 的子类。它的父类 Model 包含了所有必要的和数据库交互的方法,并提供了一个简洁漂亮的定义数据库字段的语法。 翻译
351#翻译 Believe it or not, this is all the code we need to write to have basic data access with Django. 信不信由你,这些就是我们需要编写的通过Django存取基本数据的所有代码。 翻译
353#翻译 Each model generally corresponds to a single database table, and each attribute on a model generally corresponds to a column in that database table. 每个模型相当于单个数据库表,每个属性也是这个表中的一个字段。 翻译
354#翻译 The attribute name corresponds to the column's name, and the type of field (e.g., <literal>CharField</literal> ) corresponds to the database column type (e.g., <literal>varchar</literal> ). For example, the <literal>Publisher</literal> model is equivalent to the following table (assuming PostgreSQL <literal>CREATE TABLE</literal> syntax): 属性名就是字段名,它的类型(例如 <literal>CharField</literal> )相当于数据库的字段类型 (例如 <literal>varchar</literal> )。例如, <literal>Publisher</literal> 模块等同于下面这张表(用PostgreSQL的 <literal>CREATE TABLE</literal> 语法描述): 翻译
357#翻译 Indeed, Django can generate that <literal>CREATE TABLE</literal> statement automatically, as we'll show you in a moment. 事实上,正如过一会儿我们所要展示的,Django 可以自动生成这些 <literal>CREATE TABLE</literal> 语句。 翻译
359#翻译 The exception to the one-class-per-database-table rule is the case of many-to-many relationships. “每个数据库表对应一个类”这条规则的例外情况是多对多关系。 翻译
360#翻译 In our example models, <literal>Book</literal> has a <literal>ManyToManyField</literal> called <literal>authors</literal> . This designates that a book has one or many authors, but the <literal>Book</literal> database table doesn't get an <literal>authors</literal> column. 在我们的范例模型中, <literal>Book</literal> 有一个 <literal>多对多字段</literal> 叫做 <literal>authors</literal> 。 该字段表明一本书籍有一个或多个作者,但 <literal>Book</literal> 数据库表却并没有 <literal>authors</literal> 字段。 翻译
361#翻译 Rather, Django creates an additional table a many-to-many join table that handles the mapping of books to authors. 相反,Django创建了一个额外的表(多对多连接表)来处理书籍和作者之间的映射关系。 翻译
363#翻译 For a full list of field types and model syntax options, see Appendix B. 请查看附录 B 了解所有的字段类型和模型语法选项。 翻译
365#翻译 Finally, note we haven't explicitly defined a primary key in any of these models. 最后需要注意的是,我们并没有显式地为这些模型定义任何主键。 翻译
366#翻译 Unless you instruct it otherwise, Django automatically gives every model an auto-incrementing integer primary key field called <literal>id</literal> . Each Django model is required to have a single-column primary key. 除非你单独指明,否则Django会自动为每个模型生成一个自增长的整数主键字段每个Django模型都要求有单独的主键。<literal>id</literal> 翻译
368#翻译 Installing the Model 模型安装 翻译
370#翻译 We've written the code; now let's create the tables in our database. 完成这些代码之后,现在让我们来在数据库中创建这些表。 翻译
371#翻译 In order to do that, the first step is to <emphasis>activate</emphasis> these models in our Django project. 要完成该项工作,第一步是在 Django 项目中 <emphasis>激活</emphasis> 这些模型。 翻译
372#翻译 We do that by adding the <literal>books</literal> app to the list of installed apps in the settings file. 将 <literal>books</literal> app 添加到配置文件的已安装应用列表中即可完成此步骤。 翻译
374#翻译 Edit the <literal>settings.py</literal> file again, and look for the <literal>INSTALLED_APPS</literal> setting. 再次编辑 <literal>settings.py</literal> 文件, 找到 <literal>INSTALLED_APPS</literal> 设置。 翻译
375#翻译 <literal>INSTALLED_APPS</literal> tells Django which apps are activated for a given project. <literal>INSTALLED_APPS</literal> 告诉 Django 项目哪些 app 处于激活状态。 翻译
376#翻译 By default, it looks something like this: 缺省情况下如下所示: 翻译
379#翻译 Temporarily comment out all four of those strings by putting a hash character (<literal>#</literal> ) in front of them. 把这四个设置前面加#临时注释起来。 翻译
380#翻译 (They're included by default as a common-case convenience, but we'll activate and discuss them in subsequent chapters.) While you're at it, comment out the default <literal>MIDDLEWARE_CLASSES</literal> setting, too; the default values in <literal>MIDDLEWARE_CLASSES</literal> depend on some of the apps we just commented out. (这四个app是经常使用到的,我们将在后续章节里讨论如何使用它们)。同时,注释掉MIDDLEWARE_CLASSES的默认设置条目,因为这些条目是依赖于刚才我们刚在INSTALLED_APPS注释掉的apps。 翻译
381#翻译 Then, add <literal>'mysite.books'</literal> to the <literal>INSTALLED_APPS</literal> list, so the setting ends up looking like this: 然后,添加<literal> 'mysite.books'</literal> 到<literal> INSTALLED_APPS</literal> 的末尾,此时设置的内容看起来应该是这样的: 翻译
384#翻译 (As we mentioned last chapter when setting <literal>TEMPLATE_DIRS</literal> , you'll need to be sure to include the trailing comma in <literal>INSTALLED_APPS</literal> , because it's a single-element tuple. (就像我们在上一章设置TEMPLATE_DIRS所提到的逗号,同样在INSTALLED_APPS的末尾也需添加一个逗号,因为这是个单元素的元组。 翻译
385#翻译 By the way, this book's authors prefer to put a comma after <emphasis>every</emphasis> element of a tuple, regardless of whether the tuple has only a single element. 另外,本书的作者喜欢在 <emphasis>每一个</emphasis> tuple元素后面加一个逗号,不管它是不是 只有一个元素。 翻译
386#翻译 This avoids the issue of forgetting commas, and there's no penalty for using that extra comma.) 这是为了避免忘了加逗号,而且也没什么坏处。) 翻译
388#翻译 <literal>'mysite.books'</literal> refers to the <literal>books</literal> app we're working on. <literal>'mysite.books'</literal>指示我们正在编写的<literal>books</literal> app。 翻译
389#翻译 Each app in <literal>INSTALLED_APPS</literal> is represented by its full Python path that is, the path of packages, separated by dots, leading to the app package. <literal>INSTALLED_APPS</literal> 中的每个app都使用 Python的路径描述,包的路径,用小数点“.”间隔。 翻译
391#翻译 Now that the Django app has been activated in the settings file, we can create the database tables in our database. 现在我们可以创建数据库表了。 翻译
392#翻译 First, let's validate the models by running this command: 首先,用下面的命令验证模型的有效性: 翻译
395#翻译 The <literal>validate</literal> command checks whether your models' syntax and logic are correct. <literal>validate</literal> 命令检查你的模型的语法和逻辑是否正确。 翻译
396#翻译 If all is well, you'll see the message <literal>0 errors found</literal> . If you don't, make sure you typed in the model code correctly. 如果一切正常,你会看到 <literal>0 errors found</literal> 消息。如果出错,请检查你输入的模型代码。 翻译
397#翻译 The error output should give you helpful information about what was wrong with the code. 错误输出会给出非常有用的错误信息来帮助你修正你的模型。 翻译
399#翻译 Any time you think you have problems with your models, run <literal>python manage.py validate</literal> . It tends to catch all the common model problems. 一旦你觉得你的模型可能有问题,运行 <literal>python manage.py validate</literal> 。 它可以帮助你捕获一些常见的模型定义错误。 翻译
401#翻译 If your models are valid, run the following command for Django to generate <literal>CREATE TABLE</literal> statements for your models in the <literal>books</literal> app (with colorful syntax highlighting available, if you're using Unix): 模型确认没问题了,运行下面的命令来生成 <literal>CREATE TABLE</literal> 语句(如果你使用的是Unix,那么可以启用语法高亮): 翻译
404#翻译 In this command, <literal>books</literal> is the name of the app. 在这个命令行中, <literal>books</literal> 是app的名称。 翻译
405#翻译 It's what you specified when you ran the command <literal>manage.py startapp</literal> . When you run the command, you should see something like this: 和你运行 <literal>manage.py startapp</literal> 中的一样。执行之后,输出如下: 翻译
408#翻译 Note the following: 注意: 翻译
410#翻译 Table names are automatically generated by combining the name of the app (<literal>books</literal> ) and the lowercase name of the model (<literal>publisher</literal> , <literal>book</literal> , and <literal>author</literal> ). You can override this behavior, as detailed in Appendix B. 自动生成的表名是app名称( <literal>books</literal> )和模型的小写名称 ( <literal>publisher</literal> , <literal>book</literal> , <literal>author</literal> )的组合。你可以参考附录B重写这个规则。 翻译
412#翻译 As we mentioned earlier, Django adds a primary key for each table automatically the <literal>id</literal> fields. 我们前面已经提到,Django为每个表格自动添加加了一个 <literal>id</literal> 主键, 翻译
413#翻译 You can override this, too. 你可以重新设置它。 翻译
415#翻译 By convention, Django appends <literal>&quot;_id&quot;</literal> to the foreign key field name. 按约定,Django添加 <literal>&quot;_id&quot;</literal> 后缀到外键字段名。 翻译
416#翻译 As you might have guessed, you can override this behavior, too. 你猜对了,这个同样是可以自定义的。 翻译
418#翻译 The foreign key relationship is made explicit by a <literal>REFERENCES</literal> statement. 外键是用 <literal>REFERENCES</literal> 语句明确定义的。 翻译
420#翻译 These <literal>CREATE TABLE</literal> statements are tailored to the database you're using, so database-specific field types such as <literal>auto_increment</literal> (MySQL), <literal>serial</literal> (PostgreSQL), or <literal>integer primary key</literal> (SQLite) are handled for you automatically. 这些 <literal>CREATE TABLE</literal> 语句会根据你的数据库而作调整,这样象数据库特定的一些字段例如:(MySQL),<literal>auto_increment</literal>(PostgreSQL),<literal>serial</literal>(SQLite),都会自动生成。<literal>integer primary key</literal> 翻译
421#翻译 The same goes for quoting of column names (e.g., using double quotes or single quotes). 同样的,字段名称也是自动处理(例如单引号还好是双引号)。 翻译
422#翻译 This example output is in PostgreSQL syntax. 例子中的输出是基于PostgreSQL语法的。 翻译
424#翻译 The <literal>sqlall</literal> command doesn't actually create the tables or otherwise touch your database it just prints output to the screen so you can see what SQL Django would execute if you asked it. <literal>sqlall</literal> 命令并没有在数据库中真正创建数据表,只是把SQL语句段打印出来,这样你可以看到Django究竟会做些什么。 翻译
425#翻译 If you wanted to, you could copy and paste this SQL into your database client, or use Unix pipes to pass it directly (e.g., <literal>python manage.py sqlall books | psql mydb</literal> ). However, Django provides an easier way of committing the SQL to the database: 如果你想这么做的话,你可以把那些SQL语句复制到你的数据库客户端执行,或者通过Unix管道直接进行操作(例如,<literal> python manager.py sqlall books | psql mydb</literal> )。不过,Django提供了一种更为简易的提交SQL语句至数据库的方法: 翻译
426#翻译 the <literal>syncdb</literal> command: <literal> syncdb</literal> 命令 翻译
429#翻译 Run that command, and you'll see something like this: 执行这个命令后,将看到类似以下的内容: 翻译
432#翻译 The <literal>syncdb</literal> command is a simple sync of your models to your database. <literal>syncdb</literal> 命令是同步你的模型到数据库的一个简单方法。 翻译
433#翻译 It looks at all of the models in each app in your <literal>INSTALLED_APPS</literal> setting, checks the database to see whether the appropriate tables exist yet, and creates the tables if they don't yet exist. 它会根据 <literal>INSTALLED_APPS</literal> 里设置的app来检查数据库, 如果表不存在,它就会创建它。 翻译
434#翻译 Note that <literal>syncdb</literal> does <emphasis>not</emphasis> sync changes in models or deletions of models; if you make a change to a model or delete a model, and you want to update the database, <literal>syncdb</literal> will not handle that. 需要注意的是, <literal>syncdb</literal> 并 <emphasis>不能</emphasis>将模型的修改或删除同步到数据库;如果你修改或删除了一个模型,并想把它提交到数据库,<literal>syncdb</literal>并不会做出任何处理。 翻译
435#翻译 (More on this in the Making Changes to a Database Schema section toward the end of this chapter.) (更多内容请查看本章最后的“修改数据库的架构”一段。) 翻译
437#翻译 If you run <literal>python manage.py syncdb</literal> again, nothing happens, because you haven't added any models to the <literal>books</literal> app or added any apps to <literal>INSTALLED_APPS</literal> . Ergo, it's always safe to run <literal>python manage.py syncdb</literal> it won't clobber things. 如果你再次运行 <literal>python manage.py syncdb</literal> ,什么也没发生,因为你没有添加新的模型或者 添加新的app。因此,运行<literal>python manage.py syncdb</literal>总是安全的,因为它不会重复执行SQL语句。 翻译
439#翻译 If you're interested, take a moment to dive into your database server's command-line client and see the database tables Django created. 如果你有兴趣,花点时间用你的SQL客户端登录进数据库服务器看看刚才Django创建的数据表。 翻译
440#翻译 You can manually run the command-line client (e.g., <literal>psql</literal> for PostgreSQL) or you can run the command <literal>python manage.py dbshell</literal> , which will figure out which command-line client to run, depending on your <literal>DATABASE_SERVER</literal> setting. 你可以手动启动命令行客户端(例如,执行PostgreSQL的<literal> psql</literal> 命令),也可以执行 <literal> python manage.py dbshell</literal> ,这个命令将依据<literal> DATABASE_SERVER</literal> 的里设置自动检测使用哪种命令行客户端。 翻译
441#翻译 The latter is almost always more convenient. 常言说,后来者居上。 翻译
443#翻译 Basic Data Access 基本数据访问 翻译
445#翻译 Once you've created a model, Django automatically provides a high-level Python API for working with those models. 一旦你创建了模型,Django自动为这些模型提供了高级的Python API。 翻译
446#翻译 Try it out by running <literal>python manage.py shell</literal> and typing the following: 运行 <literal>python manage.py shell</literal> 并输入下面的内容试试看: 翻译
449#翻译 These few lines of code accomplish quite a bit. 这短短几行代码干了不少的事。 翻译
450#翻译 Here are the highlights: 这里简单的说一下: 翻译
452#翻译 First, we import our <literal>Publisher</literal> model class. 首先,导入Publisher模型类, 翻译
453#翻译 This lets us interact with the database table that contains publishers. 通过这个类我们可以与包含 出版社 的数据表进行交互。 翻译
455#翻译 We create a <literal>Publisher</literal> object by instantiating it with values for each field <literal>name</literal> , <literal>address</literal> , etc. 接着,创建一个<literal> Publisher</literal> 类的实例并设置了字段<literal> name, address</literal> 等的值。 翻译
457#翻译 To save the object to the database, call its <literal>save()</literal> method. 调用该对象的 <literal>save()</literal> 方法,将对象保存到数据库中。 翻译
458#翻译 Behind the scenes, Django executes an SQL <literal>INSERT</literal> statement here. Django 会在后台执行一条 <literal>INSERT</literal> 语句。 翻译
460#翻译 To retrieve publishers from the database, use the attribute <literal>Publisher.objects</literal> , which you can think of as a set of all publishers. 最后,使用<literal> Publisher.objects</literal> 属性从数据库取出出版商的信息,这个属性可以认为是包含出版商的记录集。 翻译
461#翻译 Fetch a list of <emphasis>all</emphasis> 这个属性有许多方法, 翻译
462#翻译 <literal>Publisher</literal> objects in the database with the statement <literal>Publisher.objects.all()</literal> . Behind the scenes, Django executes an SQL <literal>SELECT</literal> statement here. 这里先介绍调用<literal> Publisher.objects.all()</literal> 方法获取数据库中<literal> Publisher</literal> 类的所有对象。这个操作的幕后,Django执行了一条SQL <literal> SELECT</literal> 语句。 翻译
464#翻译 One thing is worth mentioning, in case it wasn't clear from this example. 这里有一个值得注意的地方,在这个例子可能并未清晰地展示。 翻译
465#翻译 When youre creating objects using the Django model API, Django doesn't save the objects to the database until you call the <literal>save()</literal> method: 当你使用Django modle API创建对象时Django并未将对象保存至数据库内,除非你调用<literal> save()</literal> 方法: 翻译
468#翻译 If you want to create an object and save it to the database in a single step, use the <literal>objects.create()</literal> method. 如果需要一步完成对象的创建与存储至数据库,就使用<literal> objects.create()</literal> 方法。 翻译
469#翻译 This example is equivalent to the example above: 下面的例子与之前的例子等价: 翻译
472#翻译 Naturally, you can do quite a lot with the Django database API but first, let's take care of a small annoyance. 当然,你肯定想执行更多的Django数据库API试试看,不过,还是让我们先解决一点烦人的小问题。 翻译
474#翻译 Adding Model String Representations 添加模块的字符串表现 翻译
476#翻译 When we printed out the list of publishers, all we got was this unhelpful display that makes it difficult to tell the <literal>Publisher</literal> objects apart: 当我们打印整个publisher列表时,我们没有得到想要的有用信息,无法把<literal></literal>对象区分开来: 翻译
479#翻译 We can fix this easily by adding a method called <literal>__unicode__()</literal> to our <literal>Publisher</literal> class. 我们可以简单解决这个问题,只需要为<literal>Publisher</literal> 对象添加一个方法 <literal>__unicode__()</literal> 。 翻译
480#翻译 A <literal>__unicode__()</literal> method tells Python how to display the unicode representation of an object. <literal>__unicode__()</literal> 方法告诉Python如何将对象以unicode的方式显示出来。 翻译
481#翻译 You can see this in action by adding a <literal>__unicode__()</literal> method to the three models: 为以上三个模型添加<literal>__unicode__()</literal>方法后,就可以看到效果了: 翻译
484#翻译 As you can see, a <literal>__unicode__()</literal> method can do whatever it needs to do in order to return a representation of an object. 就象你看到的一样, <literal>__unicode__()</literal> 方法可以进行任何处理来返回对一个对象的字符串表示。 翻译
485#翻译 Here, the <literal>__unicode__()</literal> methods for <literal>Publisher</literal> and <literal>Book</literal> simply return the object's name and title, respectively, but the <literal>__unicode__()</literal> for <literal>Author</literal> is slightly more complex it pieces together the <literal>first_name</literal> and <literal>last_name</literal> fields, separated by a space. <literal>Publisher</literal>和<literal>Book</literal>对象的<literal>__unicode__()</literal>方法简单地返回各自的名称和标题,<literal>Author</literal>对象的<literal>__unicode__()</literal>方法则稍微复杂一些,它将<literal>first_name</literal>和<literal>last_name</literal>字段值以空格连接后再返回。 翻译
487#翻译 The only requirement for <literal>__unicode__()</literal> is that it return a Unicode object. 对__unicode__()的唯一要求就是它要返回一个unicode对象 翻译
488#翻译 If <literal>__unicode__()</literal> doesn't return a Unicode object if it returns, say, an integer then Python will raise a <literal>TypeError</literal> with a message like <literal>&quot;coercing to Unicode: 如果<literal> __unicode__()</literal> 方法未返回一个Unicode对象,而返回比如说一个整型数字,那么Python将抛出一个<literal> TypeError</literal> 错误,并提示:"coercing to Unicode: 翻译
489#翻译 need string or buffer, int found&quot;</literal> . need string or buffer, int found" 。 翻译
491#翻译 Unicode objects Unicode对象 翻译
493#翻译 What are Unicode objects? 什么是Unicode对象呢? 翻译
495#翻译 You can think of a Unicode object as a Python string that can handle more than a million different types of characters, from accented versions of Latin characters to non-Latin characters to curly quotes and obscure symbols. 你可以认为unicode对象就是一个Python字符串,它可以处理上百万不同类别的字符——从古老版本的Latin字符到非Latin字符,再到曲折的引用和艰涩的符号。 翻译
497#翻译 Normal Python strings are <emphasis>encoded</emphasis> , which means they use an encoding such as ASCII, ISO-8859-1 or UTF-8. 普通的python字符串是经过<emphasis>编码</emphasis>的,意思就是它们使用了某种编码方式(如ASCII,ISO-8859-1或者UTF-8)来编码。 翻译
498#翻译 If you're storing fancy characters (anything beyond the standard 128 ASCII characters such as 0-9 and A-Z) in a normal Python string, you have to keep track of which encoding your string is using, or the fancy characters might appear messed up when they're displayed or printed. 如果你把奇特的字符(其它任何超出标准128个如0-9和A-Z之类的ASCII字符)保存在一个普通的Python字符串里,你一定要跟踪你的字符串是用什么编码的,否则这些奇特的字符可能会在显示或者打印的时候出现乱码。 翻译
499#翻译 Problems occur when you have data that's stored in one encoding and you try to combine it with data in a different encoding, or you try to display it in an application that assumes a certain encoding. 当你尝试要将用某种编码保存的数据结合到另外一种编码的数据中,或者你想要把它显示在已经假定了某种编码的程序中的时候,问题就会发生。 翻译
500#翻译 We've all seen Web pages and e-mails that are littered with ??? 我们都已经见到过网页和邮件被???弄得乱七八糟。 翻译
501#翻译 ?????? ?????? 翻译
502#翻译 or other characters in odd places; that generally suggests there's an encoding problem. 或者其它出现在奇怪位置的字符:这一般来说就是存在编码问题了。 翻译
504#翻译 Unicode objects, however, have no encoding; they use a consistent, universal set of characters called, well, Unicode. 但是Unicode对象并没有编码。它们使用Unicode,一个一致的,通用的字符编码集。 翻译
505#翻译 When you deal with Unicode objects in Python, you can mix and match them safely without having to worry about encoding issues. 当你在Python中处理Unicode对象的时候,你可以直接将它们混合使用和互相匹配而不必去考虑编码细节。 翻译
507#翻译 Django uses Unicode objects throughout the framework. Django 在其内部的各个方面都使用到了 Unicode 对象。 翻译
508#翻译 Model objects are retrieved as Unicode objects, views interact with Unicode data, and templates are rendered as Unicode. 模型 对象中,检索匹配方面的操作使用的是 Unicode 对象,视图 函数之间的交互使用的是 Unicode 对象,模板的渲染也是用的 Unicode 对象。 翻译
509#翻译 Generally, you won't have to worry about making sure your encodings are right; things should just work. 通常,我们不必担心编码是否正确,后台会处理的很好。 翻译
511#翻译 Note that this has been a <emphasis>very</emphasis> high-level, dumbed down overview of Unicode objects, and you owe it to yourself to learn more about the topic. 注意,我们这里只是对Unicode对象进行非常浅显的概述,若要深入了解你可能需要查阅相关的资料。 翻译
512#翻译 A good place to start is <reference name="http://www.joelonsoftware.com/articles/Unicode.html" refuri="http://www.joelonsoftware.com/articles/Unicode.html">http://www.joelonsoftware.com/articles/Unicode.html</reference> . 这是一个很好的起点:http://www.joelonsoftware.com/articles/Unicode.html。 翻译
514#翻译 For the <literal>__unicode__()</literal> changes to take effect, exit out of the Python shell and enter it again with <literal>python manage.py shell</literal> . (This is the simplest way to make code changes take effect.) Now the list of <literal>Publisher</literal> objects is much easier to understand: 为了让我们的修改生效,先退出Python Shell,然后再次运行 <literal>python manage.py shell</literal> 进入。(这是保证代码修改生效的最简单方法。)现在<literal> Publisher</literal>对象列表容易理解多了。 翻译
517#翻译 Make sure any model you define has a <literal>__unicode__()</literal> method not only for your own convenience when using the interactive interpreter, but also because Django uses the output of <literal>__unicode__()</literal> in several places when it needs to display objects. 请确保你的每一个模型里都包含 <literal>__unicode__()</literal> 方法,这不只是为了交互时方便,也是因为 Django会在其他一些地方用 <literal>__unicode__()</literal> 来显示对象。 翻译
519#翻译 Finally, note that <literal>__unicode__()</literal> is a good example of adding <emphasis>behavior</emphasis> to models. 最后, <literal>__unicode__()</literal> 也是一个很好的例子来演示我们怎么添加 <emphasis>行为</emphasis> 到模型里。 翻译
520#翻译 A Django model describes more than the database table layout for an object; it also describes any functionality that object knows how to do. Django的模型不只是为对象定义了数据库表的结构,还定义了对象的行为。 翻译
521#翻译 <literal>__unicode__()</literal> is one example of such functionality a model knows how to display itself. <literal>__unicode__()</literal> 就是一个例子来演示模型知道怎么显示它们自己。 翻译
523#翻译 Inserting and Updating Data 插入和更新数据 翻译
525#翻译 You've already seen this done: 你已经知道怎么做了: 翻译
526#翻译 to insert a row into your database, first create an instance of your model using keyword arguments, like so: 先使用一些关键参数创建对象实例,如下: 翻译
529#翻译 As we noted above, this act of instantiating a model class does <emphasis>not</emphasis> touch the database. 这个对象实例并 <emphasis>没有</emphasis> 对数据库做修改。 翻译
530#翻译 The record isn't saved into the database until you call <literal>save()</literal> , like this: 在调用<literal> save()</literal> 方法之前,记录并没有保存至数据库,像这样: 翻译
533#翻译 In SQL, this can roughly be translated into the following: 在SQL里,这大致可以转换成这样: 翻译
536#翻译 Because the <literal>Publisher</literal> model uses an autoincrementing primary key <literal>id</literal> , the initial call to <literal>save()</literal> does one more thing: 因为 <literal>Publisher</literal> 模型有一个自动增加的主键 <literal>id</literal> ,所以第一次调用 <literal>save()</literal> 还多做了一件事: 翻译
537#翻译 it calculates the primary key value for the record and sets it to the <literal>id</literal> attribute on the instance: 计算这个主键的值并把它赋值给这个对象实例: 翻译
540#翻译 Subsequent calls to <literal>save()</literal> will save the record in place, without creating a new record (i.e., performing an SQL <literal>UPDATE</literal> statement instead of an <literal>INSERT</literal> ): 接下来再调用 <literal>save()</literal> 将不会创建新的记录,而只是修改记录内容(也就是 执行 <literal>UPDATE</literal> SQL语句,而不是 <literal>INSERT</literal> 语句): 翻译
543#翻译 The preceding <literal>save()</literal> statement will result in roughly the following SQL: 前面执行的 <literal>save()</literal> 相当于下面的SQL语句: 翻译
546#翻译 Yes, note that <emphasis>all</emphasis> of the fields will be updated, not just the ones that have been changed. 注意,并不是只更新修改过的那个字段,所有的字段都会被更新。 翻译
547#翻译 Depending on your application, this may cause a race condition. 这个操作有可能引起竞态条件,这取决于你的应用程序。 翻译
548#翻译 See Updating Multiple Objects in One Statement below to find out how to execute this (slightly different) query: 请参阅后面的“更新多个对象”小节以了解如何实现这种轻量的修改(只修改对象的部分字段)。 翻译
551#翻译 Selecting Objects 选择对象 翻译
553#翻译 Knowing how to create and update database records is essential, but chances are that the Web applications you'll build will be doing more querying of existing objects than creating new ones. 当然,创建新的数据库,并更新之中的数据是必要的,但是,对于 Web 应用程序来说,更多的时候是在检索查询数据库。 翻译
554#翻译 We've already seen a way to retrieve <emphasis>every</emphasis> record for a given model: 我们已经知道如何从一个给定的模型中取出所有记录: 翻译
557#翻译 This roughly translates to this SQL: 这相当于这个SQL语句: 翻译
560#翻译 Note 注意 翻译
562#翻译 Notice that Django doesn't use <literal>SELECT *</literal> when looking up data and instead lists all fields explicitly. 注意到Django在选择所有数据时并没有使用 <literal>SELECT*</literal> ,而是显式列出了所有字段。 翻译
563#翻译 This is by design: 设计的时候就是这样: 翻译
564#翻译 in certain circumstances <literal>SELECT *</literal> can be slower, and (more important) listing fields more closely follows one tenet of the Zen of Python: <literal>SELECT*</literal> 会更慢,而且最重要的是列出所有字段遵循了Python 界的一个信条: 翻译
565#翻译 Explicit is better than implicit. 明言胜于暗示。 翻译
567#翻译 For more on the Zen of Python, try typing <literal>import this</literal> at a Python prompt. 有关Python之禅(戒律) :-),在Python提示行输入 <literal>import this</literal> 试试看。 翻译
569#翻译 Let's take a close look at each part of this <literal>Publisher.objects.all()</literal> line: 让我们来仔细看看 <literal>Publisher.objects.all()</literal> 这行的每个部分: 翻译
571#翻译 First, we have the model we defined, <literal>Publisher</literal> . No surprise here: 首先,我们有一个已定义的模型 <literal>Publisher</literal> 。没什么好奇怪的: 翻译
572#翻译 when you want to look up data, you use the model for that data. 你想要查找数据, 你就用模型来获得数据。 翻译
574#翻译 Next, we have the <literal>objects</literal> attribute. 然后,是<literal>objects</literal>属性。 翻译
575#翻译 This is called a <emphasis>manager</emphasis> . Managers are discussed in detail in Chapter 10. 它被称为管理器,我们将在第10章中详细讨论它。 翻译
576#翻译 For now, all you need to know is that managers take care of all table-level operations on data including, most important, data lookup. 目前,我们只需了解管理器管理着所有针对数据包含、还有最重要的数据查询的表格级操作。 翻译
578#翻译 All models automatically get a <literal>objects</literal> manager; you'll use it any time you want to look up model instances. 所有的模型都自动拥有一个 <literal>objects</literal> 管理器;你可以在想要查找数据时使用它。 翻译
580#翻译 Finally, we have <literal>all()</literal> . This is a method on the <literal>objects</literal> manager that returns all the rows in the database. 最后,还有 <literal>all()</literal> 方法。这个方法返回返回数据库中所有的记录。 翻译
581#翻译 Though this object <emphasis>looks</emphasis> like a list, it's actually a <emphasis>QuerySet</emphasis> an object that represents a specific set of rows from the database. 尽管这个对象 看起来 象一个列表(list),它实际是一个 QuerySet 对象, 这个对象是数据库中一些记录的集合。 翻译
582#翻译 Appendix C deals with QuerySets in detail. 附录C将详细描述QuerySet。 翻译
583#翻译 For the rest of this chapter, we'll just treat them like the lists they emulate. 现在,我们就先当它是一个仿真列表对象好了。 翻译
585#翻译 Any database lookup is going to follow this general pattern we'll call methods on the manager attached to the model we want to query against. 所有的数据库查找都遵循一个通用模式: 翻译
587#翻译 Filtering Data 数据过滤 翻译
589#翻译 Naturally, it's rare to want to select <emphasis>everything</emphasis> from a database at once; in most cases, you'll want to deal with a subset of your data. 我们很少会一次性从数据库中取出所有的数据;通常都只针对一部分数据进行操作。 翻译
590#翻译 In the Django API, you can filter your data using the <literal>filter()</literal> method: 在Django API中,我们可以使用<literal> filter()</literal> 方法对数据进行过滤: 翻译
593#翻译 <literal>filter()</literal> takes keyword arguments that get translated into the appropriate SQL <literal>WHERE</literal> clauses. <literal>filter()</literal> 根据关键字参数来转换成 <literal>WHERE</literal> SQL语句。 翻译
594#翻译 The preceding example would get translated into something like this: 前面这个例子 相当于这样: 翻译
597#翻译 You can pass multiple arguments into <literal>filter()</literal> to narrow down things further: 你可以传递多个参数到 <literal>filter()</literal> 来缩小选取范围: 翻译
600#翻译 Those multiple arguments get translated into SQL <literal>AND</literal> clauses. 多个参数会被转换成 <literal>AND</literal> SQL从句, 翻译
601#翻译 Thus, the example in the code snippet translates into the following: 因此上面的代码可以转化成这样: 翻译
604#翻译 Notice that by default the lookups use the SQL <literal>=</literal> operator to do exact match lookups. 注意,SQL缺省的 <literal>=</literal> 操作符是精确匹配的, 翻译
605#翻译 Other lookup types are available: 其他类型的查找也可以使用: 翻译
608#翻译 That's a <emphasis>double</emphasis> underscore there between <literal>name</literal> and <literal>contains</literal> . Like Python itself, Django uses the double underscore to signal that something magic is happening here, the <literal>__contains</literal> part gets translated by Django into a SQL <literal>LIKE</literal> statement: 在 <emphasis>name</emphasis> 和 <literal>contains</literal> 之间有双下划线。和Python一样,Django也使用双下划线来表明会进行一些魔术般的操作。这里,<literal>contains</literal>部分会被Django翻译成<literal>LIKE</literal>语句: 翻译
611#翻译 Many other types of lookups are available, including <literal>icontains</literal> (case-insensitive <literal>LIKE</literal> ), <literal>startswith</literal> and <literal>endswith</literal> , and <literal>range</literal> (SQL <literal>BETWEEN</literal> queries). 其他的一些查找类型有:<literal>icontains</literal>(大小写无关的<literal>LIKE</literal>),<literal>startswith</literal>和<literal>endswith</literal>, 还有<literal>range</literal>(SQL<literal>BETWEEN</literal>查询)。 翻译
612#翻译 Appendix C describes all of these lookup types in detail. 附录C详细描述了所有的查找类型。 翻译
614#翻译 Retrieving Single Objects 获取单个对象 翻译
616#翻译 The <literal>filter()</literal> examples above all returned a <literal>QuerySet</literal> , which you can treat like a list. 上面的例子中<literal> filter()</literal> 函数返回一个记录集,这个记录集是一个列表。 翻译
617#翻译 Sometimes it's more convenient to fetch only a single object, as opposed to a list. 相对列表来说,有些时候我们更需要获取单个的对象, 翻译
618#翻译 That's what the <literal>get()</literal> method is for: <literal> get()</literal> 方法就是在此时使用的: 翻译
621#翻译 Instead of a list (rather, <literal>QuerySet</literal> ), only a single object is returned. 这样,就返回了单个对象,而不是列表(更准确的说,QuerySet)。 翻译
622#翻译 Because of that, a query resulting in multiple objects will cause an exception: 所以,如果结果是多个对象,会导致抛出异常: 翻译
625#翻译 A query that returns no objects also causes an exception: 如果查询没有返回结果也会抛出异常: 翻译
628#翻译 The <literal>DoesNotExist</literal> exception is an attribute of the model's class <literal>Publisher.DoesNotExist</literal> . In your applications, you'll want to trap these exceptions, like this: 这个 <literal>DoesNotExist</literal> 异常 是 Publisher 这个 model 类的一个属性,即 <literal>Publisher.DoesNotExist</literal>。在你的应用中,你可以捕获并处理这个异常,像这样: 翻译
631#翻译 Ordering Data 数据排序 翻译
633#翻译 As you play around with the previous examples, you might discover that the objects are being returned in a seemingly random order. 在运行前面的例子中,你可能已经注意到返回的结果是无序的。 翻译
634#翻译 You aren't imagining things; so far we haven't told the database how to order its results, so we're simply getting back data in some arbitrary order chosen by the database. 我们还没有告诉数据库 怎样对结果进行排序,所以我们返回的结果是无序的。 翻译
636#翻译 In your Django applications, you'll probably want to order your results according to a certain value say, alphabetically. 在你的 Django 应用中,你或许希望根据某字段的值对检索结果排序,比如说,按字母顺序。 翻译
637#翻译 To do this, use the <literal>order_by()</literal> method: 那么,使用 <literal>order_by()</literal> 这个方法就可以搞定了。 翻译
640#翻译 This doesn't look much different from the earlier <literal>all()</literal> example, but the SQL now includes a specific ordering: 跟以前的 <literal>all()</literal> 例子差不多,SQL语句里多了指定排序的部分: 翻译
643#翻译 You can order by any field you like: 我们可以对任意字段进行排序: 翻译
646#翻译 To order by multiple fields (where the second field is used to disambiguate ordering in cases where the first is the same), use multiple arguments: 如果需要以多个字段为标准进行排序(第二个字段会在第一个字段的值相同的情况下被使用到),使用多个参数就可以了,如下: 翻译
649#翻译 You can also specify reverse ordering by prefixing the field name with a <literal>-</literal> (that's a minus character): 我们还可以指定逆向排序,在前面加一个减号 <literal>-</literal> 前缀: 翻译
652#翻译 While this flexibility is useful, using <literal>order_by()</literal> all the time can be quite repetitive. 尽管很灵活,但是每次都要用 <literal>order_by()</literal> 显得有点啰嗦。 翻译
653#翻译 Most of the time you'll have a particular field you usually want to order by. 大多数时间你通常只会对某些 字段进行排序。 翻译
654#翻译 In these cases, Django lets you specify a default ordering in the model: 在这种情况下,Django让你可以指定模型的缺省排序方式: 翻译
657#翻译 Here, we've introduced a new concept: 现在,让我们来接触一个新的概念。 翻译
658#翻译 the <literal>class Meta</literal> , which is a class that's embedded within the <literal>Publisher</literal> class definition (i.e., it's indented to be within <literal>class Publisher</literal> ). You can use this <literal>Meta</literal> class on any model to specify various model-specific options. <literal>class Meta</literal>,内嵌于 <literal>Publisher</literal> 这个类的定义中(如果 <literal>class Publisher</literal> 是顶格的,那么 <literal>class Meta</literal> 在它之下要缩进4个空格--按 Python 的传统 )。你可以在任意一个 模型 类中使用 <literal>Meta</literal> 类,来设置一些与特定模型相关的选项。 翻译
659#翻译 A full reference of <literal>Meta</literal> options is available in Appendix B, but for now, we're concerned with the <literal>ordering</literal> option. 在 附录B 中有 <literal>Meta</literal> 中所有可选项的完整参考,现在,我们关注 <literal>ordering</literal> 这个选项就够了。 翻译
660#翻译 If you specify this, it tells Django that unless an ordering is given explicitly with <literal>order_by()</literal> , all <literal>Publisher</literal> objects should be ordered by the <literal>name</literal> field whenever they're retrieved with the Django database API. 如果你设置了这个选项,那么除非你检索时特意额外地使用了 <literal>order_by()</literal>,否则,当你使用 Django 的数据库 API 去检索时,<literal>Publisher</literal>对象的相关返回值默认地都会按 <literal>name</literal> 字段排序。 翻译
662#翻译 Chaining Lookups 连锁查询 翻译
664#翻译 You've seen how you can filter data, and you've seen how you can order it. 我们已经知道如何对数据进行过滤和排序。 翻译
665#翻译 Often, of course, you'll need to do both. 当然,通常我们需要同时进行过滤和排序查询的操作。 翻译
666#翻译 In these cases, you simply chain the lookups together: 因此,你可以简单地写成这种“链式”的形式: 翻译
669#翻译 As you might expect, this translates to a SQL query with both a <literal>WHERE</literal> and an <literal>ORDER BY</literal> : 你应该没猜错,转换成SQL查询就是 <literal>WHERE</literal> 和 <literal>ORDER BY</literal> 的组合: 翻译
672#翻译 Slicing Data 限制返回的数据 翻译
674#翻译 Another common need is to look up only a fixed number of rows. 另一个常用的需求就是取出固定数目的记录。 翻译
675#翻译 Imagine you have thousands of publishers in your database, but you want to display only the first one. 想象一下你有成千上万的出版商在你的数据库里, 但是你只想显示第一个。 翻译
676#翻译 You can do this using Python's standard list slicing syntax: 你可以使用标准的Python列表裁剪语句: 翻译
679#翻译 This translates roughly to: 这相当于: 翻译
682#翻译 Similarly, you can retrieve a specific subset of data using Python's range-slicing syntax: 类似的,你可以用Python的range-slicing语法来取出数据的特定子集: 翻译
685#翻译 This returns two objects, translating roughly to: 这个例子返回两个对象,等同于以下的SQL语句: 翻译
688#翻译 Note that negative slicing is <emphasis>not</emphasis> supported: 注意,不支持Python的负索引(negative slicing): 翻译
691#翻译 This is easy to get around, though. 虽然不支持负索引,但是我们可以使用其他的方法。 翻译
692#翻译 Just change the <literal>order_by()</literal> statement, like this: 比如,稍微修改 order_by() 语句来实现: 翻译
695#翻译 Updating Multiple Objects in One Statement 更新多个对象 翻译
697#翻译 We pointed out in the Inserting and Updating Data section that the model <literal>save()</literal> method updates <emphasis>all</emphasis> columns in a row. 在“插入和更新数据”小节中,我们有提到模型的save()方法,这个方法会更新一行里的所有列。 翻译
698#翻译 Depending on your application, you may want to update only a subset of columns. 而某些情况下,我们只需要更新行里的某几列。 翻译
700#翻译 For example, let's say we want to update the Apress <literal>Publisher</literal> to change the name from <literal>'Apress'</literal> to <literal>'Apress Publishing'</literal> . Using <literal>save()</literal> , it would look something like this: 例如说我们现在想要将Apress Publisher的名称由原来的"Apress"更改为"Apress Publishing"。若使用save()方法,如: 翻译
703#翻译 This roughly translates to the following SQL: 这等同于如下SQL语句: 翻译
706#翻译 (Note that this example assumes Apress has a publisher ID of <literal>52</literal> .) (注意在这里我们假设Apress的ID为52) 翻译
708#翻译 You can see in this example that Django's <literal>save()</literal> method sets <emphasis>all</emphasis> of the column values, not just the <literal>name</literal> column. 在这个例子里我们可以看到Django的save()方法更新了不仅仅是name列的值,还有更新了所有的列。 翻译
709#翻译 If you're in an environment where other columns of the database might change due to some other process, it's smarter to change <emphasis>only</emphasis> the column you need to change. 若name以外的列有可能会被其他的进程所改动的情况下,只更改name列显然是更加明智的。 翻译
710#翻译 To do this, use the <literal>update()</literal> method on <literal>QuerySet</literal> objects. 更改某一指定的列,我们可以调用结果集(QuerySet)对象的update()方法: 翻译
711#翻译 Here's an example: 示例如下: 翻译
714#翻译 The SQL translation here is much more efficient and has no chance of race conditions: 与之等同的SQL语句变得更高效,并且不会引起竞态条件。 翻译
717#翻译 The <literal>update()</literal> method works on any <literal>QuerySet</literal> , which means you can edit multiple records in bulk. update()方法对于任何结果集(QuerySet)均有效,这意味着你可以同时更新多条记录。 翻译
718#翻译 Here's how you might change the <literal>country</literal> from <literal>'U.S.A.'</literal> to <literal>USA</literal> in each <literal>Publisher</literal> record: 以下示例演示如何将所有Publisher的country字段值由'U.S.A'更改为'USA': 翻译
721#翻译 The <literal>update()</literal> method has a return value an integer representing how many records changed. update()方法会返回一个整型数值,表示受影响的记录条数。 翻译
722#翻译 In the above example, we got <literal>2</literal> . 在上面的例子中,这个值是2。 翻译
724#翻译 Deleting Objects 删除对象 翻译
726#翻译 To delete an object from your database, simply call the object's <literal>delete()</literal> method: 删除数据库中的对象只需调用该对象的delete()方法即可: 翻译
729#翻译 You can also delete objects in bulk by calling <literal>delete()</literal> on the result of any <literal>QuerySet</literal> . This is similar to the <literal>update()</literal> method we showed in the last section: 同样我们可以在结果集上调用delete()方法同时删除多条记录。这一点与我们上一小节提到的update()方法相似: 翻译
732#翻译 Be careful deleting your data! 删除数据时要谨慎! 翻译
733#翻译 As a precaution against deleting all of the data in a particular table, Django requires you to explicitly use <literal>all()</literal> if you want to delete <emphasis>everything</emphasis> in your table. 为了预防误删除掉某一个表内的所有数据,Django要求在删除表内所有数据时显示使用all()。 翻译
734#翻译 For example, this won't work: 比如,下面的操作将会出错: 翻译
737#翻译 But it'll work if you add the <literal>all()</literal> method: 而一旦使用all()方法,所有数据将会被删除: 翻译
740#翻译 If you're just deleting a subset of your data, you don't need to include <literal>all()</literal> . To repeat a previous example: 如果只需要删除部分的数据,就不需要调用all()方法。再看一下之前的例子: 翻译
743#翻译 What's Next? 下一章 翻译
745#翻译 Having read this chapter, you have enough knowledge of Django models to be able to write basic database applications. 通过本章的学习,你应该可以熟练地使用Django模型来编写一些简单的数据库应用程序。 翻译
746#翻译 Chapter 10 will provide some information on more advanced usage of Django's database layer. 在第十章我们将讨论Django数据库层的高级应用。 翻译
748#翻译 Once you've defined your models, the next step is to populate your database with data. 一旦你定义了你的模型,接下来就是要把数据导入数据库里了。 翻译
749#翻译 You might have legacy data, in which case Chapter 18 will give you advice about integrating with legacy databases. 你可能已经有现成的数据了,请看第十八章以获得有关如何集成现有数据库的建议。 翻译
750#翻译 You might rely on site users to supply your data, in which case Chapter 7 will teach you how to process user-submitted form data. 也可能数据是用户提供的,第七章中还会教你怎么处理用户提交的数据。 翻译
752#翻译 But in some cases, you or your team might need to enter data manually, in which case it would be helpful to have a Web-based interface for entering and managing data. 有时候,你和你的团队成员也需要手工输入数据,这时候如果有一个基于Web的数据输入和管理的界面就会很有帮助。 翻译
753#翻译 The <reference name="next chapter" refuri="../chapter06/">next chapter</reference> covers Django's admin interface, which exists precisely for that reason. 下一章将介绍解决手工录入问题的方法——Django管理界面。 翻译
755#翻译 the <reference name="GNU Free Document License" refuri="/license/">GNU Free Document License</reference>. the GNU Free Document License 翻译
756#翻译 Hosting graciously provided by Hosting graciously provided by 翻译