| ID | English原文 | 中文翻译 | 最近翻译记录 | 状态 | 操作 |
|---|---|---|---|---|---|
| 0#翻译 | Chapter 10: | 第10章: | 翻译 | ||
| 1#翻译 | Advanced Models | 数据模型高级进阶 | 翻译 | ||
| 4#翻译 | In Chapter 5, we presented an introduction to Django's database layer how to define models and how to use the database API to create, retrieve, update and delete records. | 在第5章里,我们介绍了Django的数据层如何定义数据模型以及如何使用数据库API来创建、检索、更新以及删除记录 | 翻译 | ||
| 5#翻译 | In this chapter, we'll introduce you to some more advanced features of this part of Django. | 在这章里,我们将向你介绍Django在这方面的一些更高级功能。 | 翻译 | ||
| 7#翻译 | Related Objects | 相关对象 | 翻译 | ||
| 9#翻译 | Recall our book models from Chapter 5: | 先让我们回忆一下在第五章里的关于书本(book)的数据模型: | 翻译 | ||
| 12#翻译 | As we explained in Chapter 5, accessing the value for a particular field on a database object is as straightforward as using an attribute. | 如我们在第5章的讲解,获取数据库对象的特定字段的值只需直接使用属性。 | 翻译 | ||
| 13#翻译 | For example, to determine the title of the book with ID 50, we'd do the following: | 例如,要确定ID为50的书本的标题,我们这样做: | 翻译 | ||
| 16#翻译 | But one thing we didn't mention previously is that related objects fields expressed as either a <literal>ForeignKey</literal> or <literal>ManyToManyField</literal> act slightly differently. | 但是,在之前有一件我们没提及到的是表现为<literal>ForeignKey</literal> 或 <literal>ManyToManyField</literal>的关联对象字段,它们的作用稍有不同。 | 翻译 | ||
| 18#翻译 | Accessing Foreign Key Values | 访问外键(Foreign Key)值 | 翻译 | ||
| 20#翻译 | When you access a field that's a <literal>ForeignKey</literal> , you'll get the related model object. | 当你获取一个<literal>ForeignKey</literal> 字段时,你会得到相关的数据模型对象。 | 翻译 | ||
| 21#翻译 | For example: | 例如: | 翻译 | ||
| 24#翻译 | With <literal>ForeignKey</literal> fields, it works the other way, too, but it's slightly different due to the non-symmetrical nature of the relationship. | 对于用<literal> ForeignKey</literal> 来定义的关系来说,在关系的另一端也能反向的追溯回来,只不过由于不对称性的关系而稍有不同。 | 翻译 | ||
| 25#翻译 | To get a list of books for a given publisher, use <literal>publisher.book_set.all()</literal> , like this: | 通过一个<literal> publisher</literal> 对象,直接获取 books ,用 publisher.book_set.all() ,如下: | 翻译 | ||
| 28#翻译 | Behind the scenes, <literal>book_set</literal> is just a <literal>QuerySet</literal> (as covered in Chapter 5), and it can be filtered and sliced like any other <literal>QuerySet</literal> . For example: | 实际上,<literal>book_set</literal> 只是一个 <literal>QuerySet</literal>(参考第5章的介绍),所以它可以像<literal>QuerySet</literal>一样,能实现数据过滤和分切,例如: | 翻译 | ||
| 31#翻译 | The attribute name <literal>book_set</literal> is generated by appending the lower case model name to <literal>_set</literal> . | 属性名称<literal>book_set</literal>是由模型名称的小写(如book)加<literal>_set</literal>组成的。 | 翻译 | ||
| 33#翻译 | Accessing Many-to-Many Values | 访问多对多值(Many-to-Many Values) | 翻译 | ||
| 35#翻译 | Many-to-many values work like foreign-key values, except we deal with <literal>QuerySet</literal> values instead of model instances. | 多对多和外键工作方式相同,只不过我们处理的是<literal>QuerySet</literal>而不是模型实例。 | 翻译 | ||
| 36#翻译 | For example, here's how to view the authors for a book: | 例如,这里是如何查看书籍的作者: | 翻译 | ||
| 39#翻译 | It works in reverse, too. | 反向查询也可以。 | 翻译 | ||
| 40#翻译 | To view all of the books for an author, use <literal>author.book_set</literal> , like this: | 要查看一个作者的所有书籍,使用<literal>author.book_set</literal> ,就如这样: | 翻译 | ||
| 43#翻译 | Here, as with <literal>ForeignKey</literal> fields, the attribute name <literal>book_set</literal> is generated by appending the lower case model name to <literal>_set</literal> . | 这里,就像使用 <literal>ForeignKey</literal>字段一样,属性名<literal>book_set</literal>是在数据模型(model)名后追加<literal>_set</literal>。 | 翻译 | ||
| 45#翻译 | Making Changes to a Database Schema | 更改数据库模式(Database Schema) | 翻译 | ||
| 47#翻译 | When we introduced the <literal>syncdb</literal> command in Chapter 5, we noted that <literal>syncdb</literal> merely creates tables that don't yet exist in your database it does <emphasis>not</emphasis> sync changes in models or perform deletions of models. | 在我们在第5章介绍 <literal>syncdb</literal> 这个命令时, 我们注意到 <literal>syncdb</literal>仅仅创建数据库里还没有的表,它 <emphasis>并不</emphasis> 对你数据模型的修改进行同步,也不处理数据模型的删除。 | 翻译 | ||
| 48#翻译 | If you add or change a model's field, or if you delete a model, youll need to make the change in your database manually. | 如果你新增或修改数据模型里的字段,或是删除了一个数据模型,你需要手动在数据库里进行相应的修改。 | 翻译 | ||
| 49#翻译 | This section explains how to do that. | 这段将解释了具体怎么做: | 翻译 | ||
| 51#翻译 | When dealing with schema changes, it's important to keep a few things in mind about how Django's database layer works: | 当处理模型修改的时候,将Django的数据库层的工作流程铭记于心是很重要的。 | 翻译 | ||
| 53#翻译 | Django will complain loudly if a model contains a field that has not yet been created in the database table. | 如果模型包含一个未曾在数据库里建立的字段,Django会报出错信息。 | 翻译 | ||
| 54#翻译 | This will cause an error the first time you use the Django database API to query the given table (i.e., it will happen at code execution time, not at compilation time). | 当你第一次用Django的数据库API请求表中不存在的字段时会导致错误(就是说,它会在运行时出错,而不是编译时)。 | 翻译 | ||
| 56#翻译 | Django does <emphasis>not</emphasis> care if a database table contains columns that are not defined in the model. | Django<emphasis>不</emphasis>关心数据库表中是否存在未在模型中定义的列。 | 翻译 | ||
| 58#翻译 | Django does <emphasis>not</emphasis> care if a database contains a table that is not represented by a model. | Django<emphasis>不</emphasis>关心数据库中是否存在未被模型表示的表格。 | 翻译 | ||
| 60#翻译 | Making schema changes is a matter of changing the various pieces the Python code and the database itself in the right order. | 改变模型的模式架构意味着需要按照顺序更改Python代码和数据库。 | 翻译 | ||
| 62#翻译 | Adding Fields | 添加字段 | 翻译 | ||
| 64#翻译 | When adding a field to a table/model in a production setting, the trick is to take advantage of the fact that Django doesn't care if a table contains columns that aren't defined in the model. | 当要向一个产品设置表(或者说是model)添加一个字段的时候,要使用的技巧是利用Django不关心表里是否包含model里所没有的列的特性。 | 翻译 | ||
| 65#翻译 | The strategy is to add the column in the database, and then update the Django model to include the new field. | 策略就是现在数据库里加入字段,然后同步Django的模型以包含新字段。 | 翻译 | ||
| 67#翻译 | However, there's a bit of a chicken-and-egg problem here, because in order to know how the new database column should be expressed in SQL, you need to look at the output of Django's <literal>manage.py sqlall</literal> command, which requires that the field exist in the model. | 然而 这里有一个鸡生蛋蛋生鸡的问题 ,由于要想了解新增列的SQL语句,你需要使用Django的 <literal>manage.py sqlall</literal>命令进行查看 ,而这又需要字段已经在模型里存在了。 | 翻译 | ||
| 68#翻译 | (Note that you're not <emphasis>required</emphasis> to create your column with exactly the same SQL that Django would, but it's a good idea to do so, just to be sure everything's in sync.) | (注意:你并 <emphasis>不是非得使用</emphasis>与Django相同的SQL语句创建新的字段,但是这样做确实是一个好主意 ,它能让一切都保持同步。) | 翻译 | ||
| 70#翻译 | The solution to the chicken-and-egg problem is to use a development environment instead of making the changes on a production server. | 这个鸡-蛋的问题的解决方法是在开发者环境里而不是发布环境里实现这个变化。 | 翻译 | ||
| 71#翻译 | (You <emphasis>are</emphasis> using a testing/development environment, right?) Here are the detailed steps to take. | (你<emphasis>正</emphasis>使用的是测试/开发环境,对吧?)下面是具体的实施步骤。 | 翻译 | ||
| 73#翻译 | First, take these steps in the development environment (i.e., not on the production server): | 首先,进入开发环境(也就是说,不是在发布环境里): | 翻译 | ||
| 75#翻译 | Add the field to your model. | 在你的模型里添加字段。 | 翻译 | ||
| 77#翻译 | Run <literal>manage.py sqlall [yourapp]</literal> to see the new <literal>CREATE TABLE</literal> statement for the model. | 运行 <literal>manage.py sqlall [yourapp]</literal> 来测试模型新的 <literal>CREATE TABLE</literal> 语句。 | 翻译 | ||
| 78#翻译 | Note the column definition for the new field. | 注意为新字段的列定义。 | 翻译 | ||
| 80#翻译 | Start your database's interactive shell (e.g., <literal>psql</literal> or <literal>mysql</literal> , or you can use <literal>manage.py dbshell</literal> ). Execute an <literal>ALTER TABLE</literal> statement that adds your new column. | 开启你的数据库的交互命令界面(比如, <literal>psql</literal> 或<literal>mysql</literal> , 或者可以使用 <literal>manage.py dbshell</literal> )。 执行 <literal>ALTER TABLE</literal> 语句来添加新列。 | 翻译 | ||
| 82#翻译 | Launch the Python interactive shell with <literal>manage.py shell</literal> and verify that the new field was added properly by importing the model and selecting from the table (e.g., <literal>MyModel.objects.all()[:5]</literal> ). If you updated the database correctly, the statement should work without errors. | 使用Python的<literal>manage.py shell</literal>,通过导入模型和选中表单(例如, <literal>MyModel.objects.all()[:5]</literal> )来验证新的字段是否被正确的添加 ,如果一切顺利,所有的语句都不会报错。 | 翻译 | ||
| 84#翻译 | Then on the production server perform these steps: | 然后在你的产品服务器上再实施一遍这些步骤。 | 翻译 | ||
| 86#翻译 | Start your database's interactive shell. | 启动数据库的交互界面。 | 翻译 | ||
| 88#翻译 | Execute the <literal>ALTER TABLE</literal> statement you used in step 3 of the development environment steps. | 执行在开发环境步骤中,第三步的<literal>ALTER TABLE</literal>语句。 | 翻译 | ||
| 90#翻译 | Add the field to your model. | 将新的字段加入到模型中。 | 翻译 | ||
| 91#翻译 | If you're using source-code revision control and you checked in your change in development environment step 1, now is the time to update the code (e.g., <literal>svn update</literal> , with Subversion) on the production server. | 如果你使用了某种版本控制工具,并且在第一步中,已经提交了你在开发环境上的修改,现在,可以在生产环境中更新你的代码了(例如,如果你使用Subversion,执行<literal>svn update</literal>。 | 翻译 | ||
| 93#翻译 | Restart the Web server for the code changes to take effect. | 重新启动Web server,使修改生效。 | 翻译 | ||
| 95#翻译 | For example, let's walk through what we'd do if we added a <literal>num_pages</literal> field to the <literal>Book</literal> model from Chapter 5. First, we'd alter the model in our development environment to look like this: | 让我们实践下,比如添加一个num_pages字段到第五章中Book模型。首先,我们会把开发环境中的模型改成如下形式: | 翻译 | ||
| 98#翻译 | (Note: | (注意 | 翻译 | ||
| 99#翻译 | Read the section Making Fields Optional in Chapter 6, plus the sidebar Adding NOT NULL Columns below for important details on why we included <literal>blank=True</literal> and <literal>null=True</literal> .) | 阅读第六章的“设置可选字段”以及本章下面的“添加非空列”小节以了解我们在这里添加<literal>blank=True</literal>和<literal>null=True</literal>的原因。) | 翻译 | ||
| 101#翻译 | Then we'd run the command <literal>manage.py sqlall books</literal> to see the <literal>CREATE TABLE</literal> statement. | 然后,我们运行命令manage.py sqlall books 来查看CREATE TABLE语句。 | 翻译 | ||
| 102#翻译 | Depending on your database backend, it would look something like this: | 语句的具体内容取决与你所使用的数据库, 大概是这个样子: | 翻译 | ||
| 105#翻译 | The new column is represented like this: | 新加的字段被这样表示: | 翻译 | ||
| 108#翻译 | Next, we'd start the databases interactive shell for our development database by typing <literal>psql</literal> (for PostgreSQL), and we'd execute the following statements: | 接下来,我们要在开发环境上运行数据库客户端,如果是PostgreSQL,运行 psql,,然后,我执行如下语句。 | 翻译 | ||
| 111#翻译 | Adding NOT NULL Columns | 添加 非NULL 字段 | 翻译 | ||
| 113#翻译 | There's a subtlety here that deserves mention. | 这里有个微妙之处值得一提。 | 翻译 | ||
| 114#翻译 | When we added the <literal>num_pages</literal> field to our model, we included the <literal>blank=True</literal> and <literal>null=True</literal> options. | 在我们添加字段num_pages的时候,我们使用了 blank=True 和 null=True 选项。 | 翻译 | ||
| 115#翻译 | We did this because a database column will contain NULL values when you first create it. | 这是因为在我们第一次创建它的时候,这个数据库字段会含有空值。 | 翻译 | ||
| 117#翻译 | However, it's also possible to add columns that cannot contain NULL values. | 然而,想要添加不能含有空值的字段也是可以的。 | 翻译 | ||
| 118#翻译 | To do this, you have to create the column as <literal>NULL</literal> , then populate the column's values using some default(s), and then alter the column to set the <literal>NOT NULL</literal> modifier. | 要想实现这样的效果,你必须先创建 NULL 型的字段,然后将该字段的值填充为某个默认值,然后再将该字段改为 NOT NULL 型。 | 翻译 | ||
| 119#翻译 | For example: | 例如: | 翻译 | ||
| 122#翻译 | If you go down this path, remember that you should leave off <literal>blank=True</literal> and <literal>null=True</literal> in your model (obviously). | 如果你这样做,记得你不要在模型中添加 blank=True 和 null=True 选项。 | 翻译 | ||
| 124#翻译 | After the <literal>ALTER TABLE</literal> statement, we'd verify that the change worked properly by starting the Python shell and running this code: | 执行ALTER TABLE之后,我们要验证一下修改结果是否正确。启动python并执行下面的代码: | 翻译 | ||
| 127#翻译 | If that code didn't cause errors, we'd switch to our production server and execute the <literal>ALTER TABLE</literal> statement on the production database. | 如果没有异常发生,我们将切换到生产服务器,然后在生产环境的数据库中执行命令<literal>ALTER TABLE</literal> | 翻译 | ||
| 128#翻译 | Then, we'd update the model in the production environment and restart the Web server. | 然后我们更新生产环境中的模型,最后重启web服务器。 | 翻译 | ||
| 130#翻译 | Removing Fields | 删除字段 | 翻译 | ||
| 132#翻译 | Removing a field from a model is a lot easier than adding one. | 从Model中删除一个字段要比添加容易得多。 | 翻译 | ||
| 133#翻译 | To remove a field, just follow these steps: | 删除字段,仅仅只要以下几个步骤: | 翻译 | ||
| 135#翻译 | Remove the field from your model and restart the Web server. | 删除字段,然后重新启动你的web服务器。 | 翻译 | ||
| 137#翻译 | Remove the column from your database, using a command like this: | 用以下命令从数据库中删除字段: | 翻译 | ||
| 140#翻译 | Make sure to do it in this order. | 请保证操作的顺序正确。 | 翻译 | ||
| 141#翻译 | If you remove the column from your database first, Django will immediately begin raising errors. | 如果你先从数据库中删除字段,Django将会立即抛出异常。 | 翻译 | ||
| 143#翻译 | Removing Many-to-Many Fields | 删除多对多关联字段 | 翻译 | ||
| 145#翻译 | Because many-to-many fields are different than normal fields, the removal process is different: | 由于多对多关联字段不同于普通字段,所以删除操作是不同的。 | 翻译 | ||
| 147#翻译 | Remove the <literal>ManyToManyField</literal> from your model and restart the Web server. | 从你的模型中删除<literal>ManyToManyField</literal>,然后重启web服务器。 | 翻译 | ||
| 149#翻译 | Remove the many-to-many table from your database, using a command like this: | 用下面的命令从数据库删除关联表: | 翻译 | ||
| 152#翻译 | As in the previous section, make sure to do it in this order. | 像上面一样,注意操作的顺序。 | 翻译 | ||
| 154#翻译 | Removing Models | 删除模型 | 翻译 | ||
| 156#翻译 | Removing a model entirely is as easy as removing a field. | 删除整个模型要比删除一个字段容易。 | 翻译 | ||
| 157#翻译 | To remove a model, just follow these steps: | 删除一个模型只要以下几个步骤: | 翻译 | ||
| 159#翻译 | Remove the model from your <literal>models.py</literal> file and restart the Web server. | 从文件中删除你想要删除的模型,然后重启web 服务器<literal>models.py</literal> | 翻译 | ||
| 161#翻译 | Remove the table from your database, using a command like this: | 然后用以下命令从数据库中删除表: | 翻译 | ||
| 164#翻译 | Note that you might need to remove any dependent tables from your database first e.g., any tables that have foreign keys to <literal>books_book</literal> . | 当你需要从数据库中删除任何有依赖的表时要注意(也就是任何与表books_book有外键的表 )。 | 翻译 | ||
| 166#翻译 | As in the previous sections, make sure to do it in this order. | 正如在前面部分,一定要按这样的顺序做。 | 翻译 | ||
| 168#翻译 | Managers | Managers | 翻译 | ||
| 170#翻译 | In the statement <literal>Book.objects.all()</literal> , <literal>objects</literal> is a special attribute through which you query your database. | 在语句<literal>Book.objects.all()</literal>中,<literal>objects</literal>是一个特殊的属性,需要通过它查询数据库。 | 翻译 | ||
| 171#翻译 | In Chapter 5, we briefly identified this as the model's <emphasis>manager</emphasis> . Now it's time to dive a bit deeper into what managers are and how you can use them. | 在第5章,我们只是简要地说这是模块的manager 。现在是时候深入了解managers是什么和如何使用了。 | 翻译 | ||
| 173#翻译 | In short, a model's manager is an object through which Django models perform database queries. | 总之,模块manager是一个对象,Django模块通过它进行数据库查询。 | 翻译 | ||
| 174#翻译 | Each Django model has at least one manager, and you can create custom managers in order to customize database access. | 每个Django模块至少有一个manager,你可以创建自定义manager以定制数据库访问。 | 翻译 | ||
| 176#翻译 | There are two reasons you might want to create a custom manager: | 下面是你创建自定义manager的两个原因: | 翻译 | ||
| 177#翻译 | to add extra manager methods, and/or to modify the initial <literal>QuerySet</literal> the manager returns. | 增加额外的manager方法,和/或修manager返回的初始QuerySet。 | 翻译 | ||
| 179#翻译 | Adding Extra Manager Methods | 增加额外的Manager方法 | 翻译 | ||
| 181#翻译 | Adding extra manager methods is the preferred way to add table-level functionality to your models. | 增加额外的manager方法是为模块添加表级功能的首选办法。 | 翻译 | ||
| 182#翻译 | (For row-level functionality i.e., functions that act on a single instance of a model object use model methods, which are explained later in this chapter.) | (至于行级功能,也就是只作用于模型对象实例的函数,一会儿将在本章后面解释。) | 翻译 | ||
| 184#翻译 | For example, let's give our <literal>Book</literal> model a manager method <literal>title_count()</literal> that takes a keyword and returns the number of books that have a title containing that keyword. | 例如,我们为Book模型定义了一个title_count()方法,它需要一个关键字,返回包含这个关键字的书的数量。 | 翻译 | ||
| 185#翻译 | (This example is slightly contrived, but it demonstrates how managers work.) | (这个例子有点牵强,不过它可以说明managers如何工作。) | 翻译 | ||
| 188#翻译 | With this manager in place, we can now do this: | 有了这个manager,我们现在可以这样做: | 翻译 | ||
| 191#翻译 | Here are some notes about the code: | 下面是编码该注意的一些地方: | 翻译 | ||
| 193#翻译 | We've created a <literal>BookManager</literal> class that extends <literal>django.db.models.Manager</literal> . This has a single method, <literal>title_count()</literal> , which does the calculation. | 我们建立了一个BookManager类,它继承了django.db.models.Manager。这个类只有一个title_count()方法,用来做统计。 | 翻译 | ||
| 194#翻译 | Note that the method uses <literal>self.filter()</literal> , where <literal>self</literal> refers to the manager itself. | 注意,这个方法使用了self.filter(),此处self指manager本身。 | 翻译 | ||
| 196#翻译 | We've assigned <literal>BookManager()</literal> to the <literal>objects</literal> attribute on the model. | 我们把BookManager()赋值给模型的objects属性。 | 翻译 | ||
| 197#翻译 | This has the effect of replacing the default manager for the model, which is called <literal>objects</literal> and is automatically created if you don't specify a custom manager. | 它将取代模型的默认manager(<literal>objects</literal>)如果我们没有特别定义,它将会被自动创建。 | 翻译 | ||
| 198#翻译 | We call it <literal>objects</literal> rather than something else, so as to be consistent with automatically created managers. | 我们把它命名为objects,这是为了与自动创建的manager保持一致。 | 翻译 | ||
| 200#翻译 | Why would we want to add a method such as <literal>title_count()</literal> ? To encapsulate commonly executed queries so that we don't have to duplicate code. | 为什么我们要添加一个title_count()方法呢?是为了将经常使用的查询进行封装,这样我们就不必重复编码了。 | 翻译 | ||
| 202#翻译 | Modifying Initial Manager QuerySets | 修改初始Manager QuerySets | 翻译 | ||
| 204#翻译 | A manager's base <literal>QuerySet</literal> returns all objects in the system. | manager的基本QuerySet返回系统中的所有对象。 | 翻译 | ||
| 205#翻译 | For example, <literal>Book.objects.all()</literal> returns all books in the book database. | 例如,<literal> Book.objects.all()</literal> 返回数据库book中的所有书本。 | 翻译 | ||
| 207#翻译 | You can override a manager's base <literal>QuerySet</literal> by overriding the <literal>Manager.get_query_set()</literal> method. | 我们可以通过覆盖Manager.get_query_set()方法来重写manager的基本QuerySet。 | 翻译 | ||
| 208#翻译 | <literal>get_query_set()</literal> should return a <literal>QuerySet</literal> with the properties you require. | get_query_set()按照你的要求返回一个QuerySet。 | 翻译 | ||
| 210#翻译 | For example, the following model has <emphasis>two</emphasis> managers one that returns all objects, and one that returns only the books by Roald Dahl. | 例如,下面的模型有<emphasis> 两个</emphasis> manager。一个返回所有对像,另一个只返回作者是Roald Dahl的书。 | 翻译 | ||
| 213#翻译 | With this sample model, <literal>Book.objects.all()</literal> will return all books in the database, but <literal>Book.dahl_objects.all()</literal> will only return the ones written by Roald Dahl. | 在这个示例模型中,Book.objects.all()返回了数据库中的所有书本,而Book.dahl_objects.all()只返回了一本. | 翻译 | ||
| 214#翻译 | Note that we explicitly set <literal>objects</literal> to a vanilla <literal>Manager</literal> instance, because if we hadn't, the only available manager would be <literal>dahl_objects</literal> . | 注意我们明确地将objects设置成manager的实例,因为如果我们不这么做,那么唯一可用的manager就将是dah1_objects。 | 翻译 | ||
| 216#翻译 | Of course, because <literal>get_query_set()</literal> returns a <literal>QuerySet</literal> object, you can use <literal>filter()</literal> , <literal>exclude()</literal> and all the other <literal>QuerySet</literal> methods on it. | 当然,由于get_query_set()返回的是一个QuerySet对象,所以我们可以使用filter(),exclude()和其他一切QuerySet的方法。 | 翻译 | ||
| 217#翻译 | So these statements are all legal: | 像这些语法都是正确的: | 翻译 | ||
| 220#翻译 | This example also pointed out another interesting technique: | 这个例子也指出了其他有趣的技术: | 翻译 | ||
| 221#翻译 | using multiple managers on the same model. | 在同一个模型中使用多个manager。 | 翻译 | ||
| 222#翻译 | You can attach as many <literal>Manager()</literal> instances to a model as you'd like. | 只要你愿意,你可以为你的模型添加多个manager()实例。 | 翻译 | ||
| 223#翻译 | This is an easy way to define common filters for your models. | 这是一个为模型添加通用滤器的简单方法。 | 翻译 | ||
| 225#翻译 | For example: | 例如: | 翻译 | ||
| 228#翻译 | This example allows you to request <literal>Person.men.all()</literal> , <literal>Person.women.all()</literal> , and <literal>Person.people.all()</literal> , yielding predictable results. | 这个例子允许你执行<literal> Person.men.all()</literal> ,<literal> Person.women.all()</literal> ,<literal> Person.people.all()</literal> 查询,生成你想要的结果。 | 翻译 | ||
| 230#翻译 | If you use custom <literal>Manager</literal> objects, take note that the first <literal>Manager</literal> Django encounters (in the order in which they're defined in the model) has a special status. | 如果你使用自定义的<literal>Manager</literal>对象,请注意,Django遇到的第一个Manager(以它在模型中被定义的位置为准)会有一个特殊状态。 | 翻译 | ||
| 231#翻译 | Django interprets this first <literal>Manager</literal> defined in a class as the default <literal>Manager</literal> , and several parts of Django (though not the admin application) will use that <literal>Manager</literal> exclusively for that model. | Django将会把第一个<literal>Manager</literal> 定义为默认<literal>Manager</literal> ,Django的许多部分(但是不包括admin应用)将会明确地为模型使用这个<literal>manager</literal>。 | 翻译 | ||
| 232#翻译 | As a result, it's often a good idea to be careful in your choice of default manager, in order to avoid a situation where overriding of <literal>get_query_set()</literal> results in an inability to retrieve objects you'd like to work with. | 结论是,你应该小心地选择你的默认manager。因为覆盖<literal>get_query_set()</literal> 了,你可能接受到一个无用的返回对像,你必须避免这种情况。 | 翻译 | ||
| 234#翻译 | Model methods | 模型方法 | 翻译 | ||
| 236#翻译 | Define custom methods on a model to add custom row-level functionality to your objects. | 为了给你的对像添加一个行级功能,那就定义一个自定义方法。 | 翻译 | ||
| 237#翻译 | Whereas managers are intended to do table-wide things, model methods should act on a particular model instance. | 有鉴于manager经常被用来用一些整表操作(table-wide),模型方法应该只对特殊模型实例起作用。 | 翻译 | ||
| 239#翻译 | This is a valuable technique for keeping business logic in one place the model. | 这是一项在模型的一个地方集中业务逻辑的技术。 | 翻译 | ||
| 241#翻译 | An example is the easiest way to explain this. | 最好用例子来解释一下。 | 翻译 | ||
| 242#翻译 | Here's a model with a few custom methods: | 这个模型有一些自定义方法: | 翻译 | ||
| 245#翻译 | The last method in this example is a property. | 例子中的最后一个方法是一个property。 | 翻译 | ||
| 246#翻译 | Read more about properties at <reference name="http://www.python.org/download/releases/2.2/descrintro/#property" refuri="http://www.python.org/download/releases/2.2/descrintro/#property">http://www.python.org/download/releases/2.2/descrintro/#property</reference> | 想了解更多关于属性的信息请访问<reference name="http://www.python.org/download/releases/2.2/descrintro/#property" refuri="http://www.python.org/download/releases/2.2/descrintro/#property">http://www.python.org/download/releases/2.2/descrintro/#property</reference> | 翻译 | ||
| 248#翻译 | And here's example usage: | 这是用法的实例: | 翻译 | ||
| 251#翻译 | Executing Raw SQL Queries | 执行原始SQL查询 | 翻译 | ||
| 253#翻译 | Sometimes you'll find that the Django database API can only take you so far, and you'll want to write custom SQL queries against your database. | 有时候你会发现Django数据库API带给你的也只有这么多,那你可以为你的数据库写一些自定义SQL查询。 | 翻译 | ||
| 254#翻译 | You can do this very easily by accessing the object <literal>django.db.connection</literal> , which represents the current database connection. | 你可以通过导入<literal>django.db.connection</literal>对像来轻松实现,它代表当前数据库连接。 | 翻译 | ||
| 255#翻译 | To use it, call <literal>connection.cursor()</literal> to get a cursor object. | 要使用它,需要通过<literal>connection.cursor()</literal>得到一个游标对像。 | 翻译 | ||
| 256#翻译 | Then, call <literal>cursor.execute(sql, [params])</literal> to execute the SQL and <literal>cursor.fetchone()</literal> or <literal>cursor.fetchall()</literal> to return the resulting rows. | 然后,使用<literal>cursor.execute(sql, [params])</literal>来执行SQL语句,使用<literal>cursor.fetchone()</literal>或者<literal>cursor.fetchall()</literal>来返回记录集。 | 翻译 | ||
| 257#翻译 | For example: | 例如: | 翻译 | ||
| 260#翻译 | <literal>connection</literal> and <literal>cursor</literal> mostly implement the standard Python DB-API, which you can read about at <reference name="http://www.python.org/peps/pep-0249.html" refuri="http://www.python.org/peps/pep-0249.html">http://www.python.org/peps/pep-0249.html</reference>. | <literal>connection</literal>和<literal>cursor</literal>几乎实现了标准Python DB-API,你可以访问<reference name="http://www.python.org/peps/pep-0249.html" refuri="http://www.python.org/peps/pep-0249.html"> http://www.python.org/peps/pep-0249.html</reference>来获取更多信息。 | 翻译 | ||
| 261#翻译 | If you're not familiar with the Python DB-API, note that the SQL statement in <literal>cursor.execute()</literal> uses placeholders, <literal>"%s"</literal> , rather than adding parameters directly within the SQL. | 如果你对Python DB-API不熟悉,请注意在<literal>cursor.execute()</literal> 的SQL语句中使用<literal> "%s"</literal> ,而不要在SQL内直接添加参数。 | 翻译 | ||
| 262#翻译 | If you use this technique, the underlying database library will automatically add quotes and escaping to your parameter(s) as necessary. | 如果你使用这项技术,数据库基础库将会自动添加引号,同时在必要的情况下转意你的参数。 | 翻译 | ||
| 264#翻译 | Rather than littering your view code with these <literal>django.db.connection</literal> statements, it's a good idea to put them in custom model methods or manager methods. | 不要把你的视图代码和django.db.connection语句混杂在一起,把它们放在自定义模型或者自定义manager方法中是个不错的主意。 | 翻译 | ||
| 265#翻译 | For example, the above example could be integrated into a custom manager method like this: | 比如,上面的例子可以被整合成一个自定义manager方法,就像这样: | 翻译 | ||
| 268#翻译 | And sample usage: | 然后这样使用: | 翻译 | ||
| 271#翻译 | What's Next? | 接下来做什么? | 翻译 | ||
| 273#翻译 | In the <reference name="next chapter" refuri="../chapter11/">next chapter</reference>, we'll show you Django's generic views framework, which lets you save time in building Web sites that follow common patterns. | 在<reference name="next chapter" refuri="../chapter11/">下一章</reference> 我们将讲解Django的通用视图框架,使用它创建常见的网站可以节省时间。 | 翻译 |