| ID | English原文 | 中文翻译 | 最近翻译记录 | 状态 | 操作 |
|---|---|---|---|---|---|
| 0#翻译 | Chapter 6: The Django Admin Site | 第六章 Django站点管理 | 翻译 | ||
| 3#翻译 | For a certain class of Web sites, an <emphasis>admin interface</emphasis> is an essential part of the infrastructure. | 对于某一类网站, <emphasis>管理界面</emphasis> 是基础设施中非常重要的一部分。 | 翻译 | ||
| 4#翻译 | This is a Web-based interface, limited to trusted site administrators, that enables the adding, editing and deletion of site content. | 这是一个基于网页的,只向可信任的管理者开放的界面,它可以让你添加,编辑和删除网站内容。 | 翻译 | ||
| 5#翻译 | Some common examples: | 一些常见的例子: | 翻译 | ||
| 6#翻译 | the interface you use to post to your blog, the backend site managers use to moderate user-generated comments, the tool your clients use to update the press releases on the Web site you built for them. | 你可以用这个界面发布博客,后台的网站管理者用它来润色读者提交的内容,你的客户用你给他们建立的界面工具更新新闻并发布在网站上,这些都是使用管理界面的例子。 | 翻译 | ||
| 8#翻译 | There's a problem with admin interfaces, though: | 但是管理界面有一问题: | 翻译 | ||
| 9#翻译 | it's boring to build them. | 创建它太繁琐。 | 翻译 | ||
| 10#翻译 | Web development is fun when you're developing public-facing functionality, but building admin interfaces is always the same. | 当你开发对公众的功能时,网页开发是有趣的,但是创建管理界面通常是千篇一律的。 | 翻译 | ||
| 11#翻译 | You have to authenticate users, display and handle forms, validate input, and so on. | 你必须认证用户,显示并管理表格,验证输入的有效性诸如此类。 | 翻译 | ||
| 12#翻译 | It's boring, and its repetitive. | 这很繁琐而且是重复劳动。 | 翻译 | ||
| 14#翻译 | So what's Djangos approach to these boring, repetitive tasks? | Django 在对这些繁琐和重复的工作进行了哪些改进? | 翻译 | ||
| 15#翻译 | It does it all for you in just a couple of lines of code, no less. | 它用不能再少的代码为你做了所有的一切。 | 翻译 | ||
| 16#翻译 | With Django, building an admin interface is a solved problem. | Django 中创建管理界面已经不是问题。 | 翻译 | ||
| 18#翻译 | This chapter is about Django's automatic admin interface. | 这一章是关于 Django 的自动管理界面。 | 翻译 | ||
| 19#翻译 | The feature works by reading metadata in your model to provide a powerful and production-ready interface that site administrators can start using immediately. | 这个特性是这样起作用的:它读取你模式中的元数据,然后提供给你一个强大而且可以使用的界面,网站管理者可以马上使用它。 | 翻译 | ||
| 20#翻译 | Here, we discuss how to activate, use, and customize this feature. | 我们在这里讨论如何激活,使用和自定义这些特性。 | 翻译 | ||
| 22#翻译 | Note that we recommend reading this chapter even if you don't intend to use the Django admin site, because we introduce a few concepts that apply to all of Django, regardless of admin-site usage. | 请注意我们建议你读这章,即使你不打算用admin。因为我们将介绍一些概念,这些概念可以应用到Django的所有方面,而不仅仅是admin。 | 翻译 | ||
| 24#翻译 | The django.contrib packages | django.contrib 包 | 翻译 | ||
| 26#翻译 | Django's automatic admin is part of a larger suite of Django functionality called <literal>django.contrib</literal> the part of the Django codebase that contains various useful add-ons to the core framework. | Django自动管理工具是django.contrib的一部分。django.contrib是一套庞大的功能集,它是Django基本代码的组成部分,Django框架就是由众多包含附加组件(add-on)的基本代码构成的。 | 翻译 | ||
| 27#翻译 | You can think of <literal>django.contrib</literal> as Django's equivalent of the Python standard library optional, de facto implementations of common patterns. | 你可以把django.contrib看作是可选的Python标准库或普遍模式的实际实现。 | 翻译 | ||
| 28#翻译 | They're bundled with Django so that you dont have to reinvent the wheel in your own applications. | 它们与Django捆绑在一起,这样你在开发中就不用“重复发明轮子”了。 | 翻译 | ||
| 30#翻译 | The admin site is the first part of <literal>django.contrib</literal> that we're covering in this book; technically, it's called <literal>django.contrib.admin</literal> . Other available features in <literal>django.contrib</literal> include a user authentication system (<literal>django.contrib.auth</literal> ), support for anonymous sessions (<literal>django.contrib.sessions</literal> ) and even a system for user comments (<literal>django.contrib.comments</literal> ). You'll get to know the various <literal>django.contrib</literal> features as you become a Django expert, and we'll spend some more time discussing them in Chapter 16. | 管理工具是本书讲述django.contrib的第一个部分。从技术层面上讲,它被称作django.contrib.admin。django.contrib中其它可用的特性,如用户身份认证系统(django.contrib.auth)、支持匿名会话(django.contrib.sessioins)甚至用户评论系统(django.contrib.comments)。这些,我们将在第十六章详细讨论。在成为一个Django专家以前,你将会知道更多django.contrib的特性。 | 翻译 | ||
| 31#翻译 | For now, just know that Django ships with many nice add-ons, and <literal>django.contrib</literal> is generally where they live. | 目前,你只需要知道Django自带很多优秀的附加组件,它们都存在于django.contrib包里。 | 翻译 | ||
| 33#翻译 | Activating the Admin Interface | 激活管理界面 | 翻译 | ||
| 35#翻译 | The Django admin site is entirely optional, because only certain types of sites need this functionality. | Django管理站点完全是可选择的,因为仅仅某些特殊类型的站点才需要这些功能。 | 翻译 | ||
| 36#翻译 | That means you'll need to take a few steps to activate it in your project. | 这意味着你需要在你的项目中花费几个步骤去激活它。 | 翻译 | ||
| 38#翻译 | First, make a few changes to your settings file: | 第一步,对你的settings.py文件做如下改变: | 翻译 | ||
| 40#翻译 | Add <literal>'django.contrib.admin'</literal> to the <literal>INSTALLED_APPS</literal> setting. | 将<literal>'django.contrib.admin'</literal>加入<literal>INSTALLED_APPS</literal>配置项中。 | 翻译 | ||
| 41#翻译 | (The order of <literal>INSTALLED_APPS</literal> doesn't matter, but we like to keep things alphabetical so it's easy for a human to read.) | (<literal>INSTALLED_APPS</literal>中的配置顺序是没有关系的, 但是我们喜欢保持按字母顺序排序以方便阅读。) | 翻译 | ||
| 43#翻译 | Make sure <literal>INSTALLED_APPS</literal> contains <literal>'django.contrib.auth'</literal> , <literal>'django.contrib.contenttypes'</literal> and <literal>'django.contrib.sessions'</literal> . The Django admin site requires these three packages. | 保证<literal>INSTALLED_APPS</literal>中包含<literal>'django.contrib.auth'</literal>,<literal>'django.contrib.contenttypes'</literal>和<literal>'django.contrib.sessions'</literal>,Django的管理工具需要这3个包。 | 翻译 | ||
| 44#翻译 | (If you're following along with our ongoing <literal>mysite</literal> project, note that we commented out these three <literal>INSTALLED_APPS</literal> entries in Chapter 5. Uncomment them now.) | (如果你一直跟随本文制作mysite项目的话,那么请注意我们在第五章的时候把这三项INSTALLED_APPS条目注释掉了。现在,请把注释取消。) | 翻译 | ||
| 46#翻译 | Make sure <literal>MIDDLEWARE_CLASSES</literal> contains <literal>'django.middleware.common.CommonMiddleware'</literal> , <literal>'django.contrib.sessions.middleware.SessionMiddleware'</literal> and <literal>'django.contrib.auth.middleware.AuthenticationMiddleware'</literal> . (Again, if you're following along, note that we commented them out in Chapter 5, so uncomment them.) | 确保<literal>MIDDLEWARE_CLASSES</literal> 包含<literal>'django.middleware.common.CommonMiddleware'</literal> 、<literal>'django.contrib.sessions.middleware.SessionMiddleware'</literal> 和<literal>'django.contrib.auth.middleware.AuthenticationMiddleware'</literal> 。(再次提醒,如果你是顺序学习本文的话,请把在第五章做的注释取消。) | 翻译 | ||
| 48#翻译 | Second, run <literal>python manage.py syncdb</literal> . This step will install the extra database tables that the admin interface uses. | 运行 <literal>python manage.py syncdb</literal> 。这一步将生成管理界面使用的额外数据库表。 | 翻译 | ||
| 49#翻译 | The first time you run <literal>syncdb</literal> with <literal>'django.contrib.auth'</literal> in <literal>INSTALLED_APPS</literal> , you'll be asked about creating a superuser. | 当你把<literal>'django.contrib.auth'</literal>加进<literal>INSTALLED_APPS</literal>后,第一次运行<literal>syncdb</literal>命令时, 系统会请你创建一个超级用户。 | 翻译 | ||
| 50#翻译 | If you don't do this, youll need to run <literal>python manage.py createsuperuser</literal> separately to create an admin user account; otherwise, you won't be able to log in to the admin site. | 如果你不这么作,你需要单独运行<literal>python manage.py createsuperuser</literal>来创建一个admin的用户帐号,否则你将不能登录到admin站点。 | 翻译 | ||
| 51#翻译 | (Potential gotcha: | (提醒一句: | 翻译 | ||
| 52#翻译 | the <literal>python manage.py createsuperuser</literal> command is only available if <literal>'django.contrib.auth'</literal> is in your <literal>INSTALLED_APPS</literal> .) | 只有当<literal>INSTALLED_APPS</literal>包含<literal>'django.contrib.auth'</literal>时,<literal>python manage.py createsuperuser</literal>这个命令才可用.) | 翻译 | ||
| 54#翻译 | Third, add the admin site to your URLconf (in <literal>urls.py</literal> , remember). | 第三,将admin访问配置在URLconf(记住,在<literal>urls.py</literal>中). | 翻译 | ||
| 55#翻译 | By default, the <literal>urls.py</literal> generated by <literal>django-admin.py startproject</literal> contains commented-out code for the Django admin, and all you have to do is uncomment it. | 默认情况下,命令<literal>django-admin.py startproject</literal>生成的文件<literal>urls.py</literal>是将Django admin的路径注释掉的,你所要做的就是取消注释。 | 翻译 | ||
| 56#翻译 | For the record, here are the bits you need to make sure are in there: | 请注意,以下内容是必须确保存在的: | 翻译 | ||
| 59#翻译 | With that bit of configuration out of the way, now you can see the Django admin site in action. | 当这一切都配置好后,现在你会发现Django管理工具可以运行了。 | 翻译 | ||
| 60#翻译 | Just run the development server (<literal>python manage.py runserver</literal> , as in previous chapters) and visit <literal>http://127.0.0.1:8000/admin/</literal> in your Web browser. | 启动开发服务器(如前:<literal> python manage.py runserver</literal> ),然后在浏览器中访问:<literal>http://127.0.0.1:8000/admin/</literal> | 翻译 | ||
| 62#翻译 | Using the Admin Site | 使用管理工具。 | 翻译 | ||
| 64#翻译 | The admin site is designed to be used by nontechnical users, and as such it should be pretty self-explanatory. | 管理界面的设计是针对非技术人员的,所以它应该一目了然。 | 翻译 | ||
| 65#翻译 | Nevertheless, we'll give you a quick walkthrough of the basic features. | 尽管如此,这里简单介绍一下它的基本特性。 | 翻译 | ||
| 67#翻译 | The first thing you'll see is a login screen, as shown in Figure 6-1. | 你看到的第一件事是如图6-1所示的登录屏幕。 | 翻译 | ||
| 69#翻译 | <image alt="Screenshot of Django's login page." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/login.png"/><cnid>24 | <image alt="Django 登录页面的截图。" uri="http://new-media.djangobook.com/content/en/1.0/chapter06/login.png"/><cnid>30 | 翻译 | ||
| 70#翻译 | Figure 6-1. | 图 6-1. | 翻译 | ||
| 71#翻译 | Django's login screen | Django的登录界面 | 翻译 | ||
| 73#翻译 | Log in with the username and password you set up when you added your superuser. | 使用你之前设置的超级用户的用户名和密码登录。 | 翻译 | ||
| 74#翻译 | If you're unable to log in, make sure you've actually created a superuser try running <literal>python manage.py createsuperuser</literal> . | 如果无法登录,请运行<literal> python manage.py createsuperuser</literal> ,确保你已经创建了一个超级用户。 | 翻译 | ||
| 76#翻译 | Once you're logged in, the first thing you'll see will be the admin home page. | 一旦登录了,你将看到admin首页。 | 翻译 | ||
| 77#翻译 | This page lists all the available types of data that can be edited on the admin site. | 这个页面列出了管理工具中可编辑的所有数据类型。 | 翻译 | ||
| 78#翻译 | At this point, because we haven't activated any of our own models yet, the list is sparse: | 现在,由于我们还没有激活任何模块,所以这个列表只有寥寥数条项目: | 翻译 | ||
| 79#翻译 | it includes only Groups and Users, which are the two default admin-editable models. | 它仅有两个默认的管理-编辑模块:用户组(Groups)和用户(Users)。 | 翻译 | ||
| 81#翻译 | <image alt="Screenshot of the Django admin home page." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/admin_index.png"/><cnid>28 | <image alt="Django 主管理索引截图。" uri="http://new-media.djangobook.com/content/en/1.0/chapter06/admin_index.png"/><cnid>35 | 翻译 | ||
| 82#翻译 | Figure 6-2. | 图 6-2。 | 翻译 | ||
| 83#翻译 | The Django admin home page | Django admin的首页 | 翻译 | ||
| 85#翻译 | Each type of data in the Django admin site has a <emphasis>change list</emphasis> and an <emphasis>edit form</emphasis> . Change lists show you all the available objects in the database, and edit forms let you add, change or delete particular records in your database. | 在Django管理页面中,每一种数据类型都有一个<emphasis> change list</emphasis> 和<emphasis> edit form</emphasis> 。前者显示数据库中所有的可用对象;后者可让你添加、更改和删除数据库中的某条记录。 | 翻译 | ||
| 87#翻译 | Other languages | 其它语言 | 翻译 | ||
| 89#翻译 | If your primary language is not English and your Web browser is configured to prefer a language other than English, you can make a quick change to see whether the Django admin site has been translated into your language. | 如果你的母语不是英语,并且你的浏览器的默认语言也不是英语,你可以做一个快速更改来观察Django管理工具是否被翻译成你想要的语言。 | 翻译 | ||
| 90#翻译 | Just add <literal>'django.middleware.locale.LocaleMiddleware'</literal> to your <literal>MIDDLEWARE_CLASSES</literal> setting, making sure it appears <emphasis>after</emphasis> | 仅需添加<literal> 'django.middleware.locale.LocaleMiddleware'</literal> 到<literal> MIDDLEWARE_CLASSES</literal> 设置中,并确保它在 | 翻译 | ||
| 91#翻译 | <literal>'django.contrib.sessions.middleware.SessionMiddleware'</literal> . | 'django.contrib.sessions.middleware.SessionMiddleware'之后。 | 翻译 | ||
| 93#翻译 | When you've done that, reload the admin index page. | 完成后,请刷新页面。 | 翻译 | ||
| 94#翻译 | If a translation for your language is available, then the various parts of the interface from the Change password and Log out links at the top of the page, to the Groups and Users links in the middle will appear in your language instead of English. | 如果你设置的语言可用,一系列的链接文字将被显示成这种语言。这些文字包括页面顶端的Change password和Log out,页面中部的Groups和Users。 | 翻译 | ||
| 95#翻译 | Django ships with translations for dozens of languages. | Django自带了多种语言的翻译。 | 翻译 | ||
| 97#翻译 | For much more on Django's internationalization features, see Chapter 19. | 关于Django更多的国际化特性,请参见第十九章。 | 翻译 | ||
| 99#翻译 | Click the Change link in the Users row to load the change list page for users. | 点击Uers行中的Change链接,引导用户更改列表。 | 翻译 | ||
| 101#翻译 | <image alt="Screenshot of the user change list page." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/user_changelist.png"/><cnid>36 | <image alt="修改过的变更列表页面截图。" uri="http://new-media.djangobook.com/content/en/1.0/chapter06/changelist2.png"/><cnid>76 | 翻译 | ||
| 102#翻译 | Figure 6-3. | 图 6-3. | 翻译 | ||
| 103#翻译 | The user change list page | 用户更改列表页面 | 翻译 | ||
| 105#翻译 | This page displays all users in the database; you can think of it as a prettied-up Web version of a <literal>SELECT * FROM auth_user;</literal> SQL query. | 这个页面显示了数据库中所有的用户。你可以将它看作是<literal>SELECT * FROM auth_user;</literal>的一个漂亮的网页版查询。 | 翻译 | ||
| 106#翻译 | If you're following along with our ongoing example, you'll only see one user here, assuming you've added only one, but once you have more users, you'll probably find the filtering, sorting and searching options useful. | 如果你一直跟着我们的例子做练习,并且只添加了一个用户,你会在这个页面中看到一个用户。但是如果你添加了多个用户,你会发现页面中还有过滤器、排序和查询框。 | 翻译 | ||
| 107#翻译 | Filtering options are at right, sorting is available by clicking a column header, and the search box at the top lets you search by username. | 过滤器在右边;排序功能可通过点击列头查看;查询框在页面顶部,它允许你通过用户名查询。 | 翻译 | ||
| 109#翻译 | Click the username of the user you created, and you'll see the edit form for that user. | 点击其中一个用户名,你会看见关于这个用户的编辑窗口。 | 翻译 | ||
| 111#翻译 | <image alt="Screenshot of the user edit form" uri="http://new-media.djangobook.com/content/en/2.0/chapter06/user_editform.png"/><cnid>40 | <image alt="典型的编辑表格截图。" uri="http://new-media.djangobook.com/content/en/1.0/chapter06/editform.png"/><cnid>44 | 翻译 | ||
| 112#翻译 | Figure 6-4. | 图 6-4. | 翻译 | ||
| 113#翻译 | The user edit form | 典型的编辑表格 | 翻译 | ||
| 115#翻译 | This page lets you change the attributes of the user, like the first/last names and various permissions. | 这个页面允许你修改用户的属性,如姓名和权限。 | 翻译 | ||
| 116#翻译 | (Note that to change a user's password, you should click change password form under the password field rather than editing the hashed code.) Another thing to note here is that fields of different types get different widgets for example, date/time fields have calendar controls, boolean fields have checkboxes, character fields have simple text input fields. | (如果要更改用户密码,你必须点击密码字段下的change password form,而不是直接更改字段值中的哈希码。)另外需要注意的是,不同类型的字段会用不同的窗口控件显示。例如,日期/时间型用日历控件,布尔型用复选框,字符型用简单文本框显示。 | 翻译 | ||
| 118#翻译 | You can delete a record by clicking the delete button at the bottom left of its edit form. | 你可以通过点击编辑页面下方,edit form左边的删除按钮来删除一条记录。 | 翻译 | ||
| 119#翻译 | That'll take you to a confirmation page, which, in some cases, will display any dependent objects that will be deleted, too. | 你会见到一个确认页面。有时候,它会显示有哪些关联的对象将会一并被删除。 | 翻译 | ||
| 120#翻译 | (For example, if you delete a publisher, any book with that publisher will be deleted, too!) | (例如,如果你要删除一个出版社,它下面所有的图书也将被删除。) | 翻译 | ||
| 122#翻译 | You can add a record by clicking Add in the appropriate column of the admin home page. | 你可以通过点击管理主页面中某个对象的Add来添加一条新记录。 | 翻译 | ||
| 123#翻译 | This will give you an empty version of the edit page, ready for you to fill out. | 一个空白记录的页面将被打开,等待你填充。 | 翻译 | ||
| 125#翻译 | You'll also notice that the admin interface also handles input validation for you. | 你还能看到管理界面也控制着你输入的有效性。 | 翻译 | ||
| 126#翻译 | Try leaving a required field blank or putting an invalid date into a date field, and you'll see those errors when you try to save, as shown in Figure 6-5. | 你可以试试不填必需的栏目或者在时间栏里填错误的时间,你会发现当你要保存时会出现错误信息,如图6-5所示。 | 翻译 | ||
| 128#翻译 | <image alt="Screenshot of an edit form displaying errors." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/user_editform_errors.png"/><cnid>46 | <image alt="编辑表格显示错误信息的截图。" uri="http://new-media.djangobook.com/content/en/1.0/chapter06/editform_errors.png"/><cnid>48 | 翻译 | ||
| 129#翻译 | Figure 6-5. | 图6-5. | 翻译 | ||
| 130#翻译 | An edit form displaying errors | 编辑表格显示错误信息 | 翻译 | ||
| 132#翻译 | When you edit an existing object, you'll notice a History link in the upper-right corner of the window. | 当你编辑已有的对像时,你在窗口的右上角可以看到一个历史按钮。 | 翻译 | ||
| 133#翻译 | Every change made through the admin interface is logged, and you can examine this log by clicking the History link (see Figure 6-6). | 通过管理界面做的每一个改变都留有记录,你可以点击历史链接来检查这个记录(见图6-6)。 | 翻译 | ||
| 135#翻译 | <image alt="Screenshot of an object history page." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/user_history.png"/><cnid>49 | <image alt="Django 历史页面截图。" uri="http://new-media.djangobook.com/content/en/1.0/chapter06/history.png"/><cnid>52 | 翻译 | ||
| 136#翻译 | Figure 6-6. | 图6-6. | 翻译 | ||
| 137#翻译 | An object history page | Django 对像历史页面 | 翻译 | ||
| 139#翻译 | Adding Your Models to the Admin Site | 将你的Models加入到Admin管理中 | 翻译 | ||
| 141#翻译 | There's one crucial part we haven't done yet. | 有一个关键步骤我们还没做。 | 翻译 | ||
| 142#翻译 | Lets add our own models to the admin site, so we can add, change and delete objects in our custom database tables using this nice interface. | 让我们将自己的模块加入管理工具中,这样我们就能够通过这个漂亮的界面添加、修改和删除数据库中的对象了。 | 翻译 | ||
| 143#翻译 | We'll continue the <literal>books</literal> example from Chapter 5, where we defined three models: | 我们将继续第五章中的<literal> book</literal> 例子。在其中,我们定义了三个模块: | 翻译 | ||
| 144#翻译 | <literal>Publisher</literal> , <literal>Author</literal> and <literal>Book</literal> . | <literal>Publisher</literal> 、 <literal>Author</literal> 和 <literal>Book</literal> 。 | 翻译 | ||
| 146#翻译 | Within the <literal>books</literal> directory (<literal>mysite/books</literal> ), create a file called <literal>admin.py</literal> , and type in the following lines of code: | 在<literal> books</literal> 目录下(<literal> mysite/books</literal> ),创建一个文件:<literal> admin.py</literal> ,然后输入以下代码: | 翻译 | ||
| 149#翻译 | This code tells the Django admin site to offer an interface for each of these models. | 这些代码通知管理工具为这些模块逐一提供界面。 | 翻译 | ||
| 151#翻译 | Once you've done this, go to your admin home page in your Web browser (<literal>http://127.0.0.1:8000/admin/</literal> ), and you should see a Books section with links for Authors, Books and Publishers. | 完成后,打开页面 <literal> http://127.0.0.1:8000/admin/</literal> ,你会看到一个Books区域,其中包含Authors、Books和Publishers。 | 翻译 | ||
| 152#翻译 | (You might have to stop and start the <literal>runserver</literal> for the changes to take effect.) | (你可能需要先停止,然后再启动服务(<literal> runserver</literal> ),才能使其生效。) | 翻译 | ||
| 154#翻译 | You now have a fully functional admin interface for each of those three models. | 现在你拥有一个功能完整的管理界面来管理这三个模块了。 | 翻译 | ||
| 155#翻译 | That was easy! | 很简单吧! | 翻译 | ||
| 157#翻译 | Take some time to add and change records, to populate your database with some data. | 花点时间添加和修改记录,以填充数据库。 | 翻译 | ||
| 158#翻译 | If you followed Chapter 5's examples of creating <literal>Publisher</literal> objects (and you didn't delete them), youll already see those records on the publisher change list page. | 如果你跟着第五章的例子一起创建Publisher对象的话(并且没有删除),你会在列表中看到那些记录。 | 翻译 | ||
| 160#翻译 | One feature worth mentioning here is the admin site's handling of foreign keys and many-to-many relationships, both of which appear in the <literal>Book</literal> model. | 这里需要提到的一个特性是,管理工具处理外键和多对多关系(这两种关系可以在<literal> Book</literal> 模块中找到)的方法。 | 翻译 | ||
| 161#翻译 | As a reminder, here's what the <literal>Book</literal> model looks like: | 作为提醒,这里有个<literal> Book</literal> 模块的例子: | 翻译 | ||
| 164#翻译 | On the Django admin site's Add book page (<literal>http://127.0.0.1:8000/admin/books/book/add/</literal> ), the publisher (a <literal>ForeignKey</literal> ) is represented by a select box, and the authors field (a <literal>ManyToManyField</literal> ) is represented by a multiple-select box. | 在Add book页面中(<literal> http://127.0.0.1:8000/admin/books/book/add/</literal> ),<literal> 外键</literal> publisher用一个选择框显示,<literal> 多对多</literal> 字段author用一个多选框显示。 | 翻译 | ||
| 165#翻译 | Both fields sit next to a green plus sign icon that lets you add related records of that type. | 点击两个字段后面的绿色加号,可以让你添加相关的记录。 | 翻译 | ||
| 166#翻译 | For example, if you click the green plus sign next to the Publisher field, you'll get a pop-up window that lets you add a publisher. | 举个例子,如果你点击Publisher后面的加号,你将会得到一个弹出窗口来添加一个publisher。 | 翻译 | ||
| 167#翻译 | After you successfully create the publisher in the pop-up, the Add book form will be updated with the newly created publisher. | 当你在那个窗口中成功创建了一个publisher后,Add book表单会自动把它更新到字段上去。 | 翻译 | ||
| 168#翻译 | Slick. | 很方便吧。 | 翻译 | ||
| 170#翻译 | How the Admin Site Works | Admin是如何工作的 | 翻译 | ||
| 172#翻译 | Behind the scenes, how does the admin site work? | 在幕后,管理工具是如何工作的呢? | 翻译 | ||
| 173#翻译 | It's pretty straightforward. | 其实很简单。 | 翻译 | ||
| 175#翻译 | When Django loads your URLconf from <literal>urls.py</literal> at server startup, it executes the <literal>admin.autodiscover()</literal> statement that we added as part of activating the admin. | 当服务启动时,Django从<literal> urls.py</literal> 引导URLconf,然后执行<literal> admin.autodiscover()</literal> 语句。 | 翻译 | ||
| 176#翻译 | This function iterates over your <literal>INSTALLED_APPS</literal> setting and looks for a file called <literal>admin.py</literal> in each installed app. | 这个函数遍历<literal>INSTALLED_APPS</literal>配置,并且寻找相关的 <literal>admin.py</literal>文件。 | 翻译 | ||
| 177#翻译 | If an <literal>admin.py</literal> exists in a given app, it executes the code in that file. | 如果在指定的app目录下找到<literal>admin.py</literal>,它就执行其中的代码。 | 翻译 | ||
| 179#翻译 | In the <literal>admin.py</literal> in our <literal>books</literal> app, each call to <literal>admin.site.register()</literal> simply registers the given model with the admin. | 在<literal> books</literal> 应用程序目录下的<literal> admin.py</literal> 文件中,每次调用<literal> admin.site.register()</literal> 都将那个模块注册到管理工具中。 | 翻译 | ||
| 180#翻译 | The admin site will only display an edit/change interface for models that have been explicitly registered. | 管理工具只为那些明确注册了的模块显示一个编辑/修改的界面。 | 翻译 | ||
| 182#翻译 | The app <literal>django.contrib.auth</literal> includes its own <literal>admin.py</literal> , which is why Users and Groups showed up automatically in the admin. | 应用程序<literal> django.contrib.auth</literal> 包含自身的<literal> admin.py</literal> ,所以Users和Groups能在管理工具中自动显示。 | 翻译 | ||
| 183#翻译 | Other <literal>django.contrib</literal> apps, such as <literal>django.contrib.redirects</literal> , also add themselves to the admin, as do many third-party Django applications you might download from the Web. | 其它的<literal>django.contrib</literal>应用程序,如<literal>django.contrib.redirects</literal>,像从网上下载的第三方Django应用程序一样,都会自行添加到管理工具中。 | 翻译 | ||
| 185#翻译 | Beyond that, the Django admin site is just a Django application, with its own models, templates, views and URLpatterns. | 综上所述,管理工具其实就是一个Django应用程序,包含自己的模块、模板、视图和URLpatterns。 | 翻译 | ||
| 186#翻译 | You add it to your application by hooking it into your URLconf, just as you hook in your own views. | 你要像添加自己的视图一样,把它添加到URLconf里面。 | 翻译 | ||
| 187#翻译 | You can inspect its templates, views and URLpatterns by poking around in <literal>django/contrib/admin</literal> in your copy of the Django codebase but don't be tempted to change anything directly in there, as there are plenty of hooks for you to customize the way the admin site works. | 你可以在Django基本代码中的<literal>django/contrib/admin</literal> 目录下,检查它的模板、视图和URLpatterns,但你不要尝试直接修改其中的任何代码,因为里面有很多地方可以让你自定义管理工具的工作方式。 | 翻译 | ||
| 188#翻译 | (If you do decide to poke around the Django admin application, keep in mind it does some rather complicated things in reading metadata about models, so it would probably take a good amount of time to read and understand the code.) | (如果你确实想浏览Django管理工具的代码,请谨记它在读取关于模块的元数据过程中做了些不简单的工作,因此最好花些时间阅读和理解那些代码。) | 翻译 | ||
| 190#翻译 | Making Fields Optional | 设置字段可选 | 翻译 | ||
| 192#翻译 | After you play around with the admin site for a while, you'll probably notice a limitation the edit forms require every field to be filled out, whereas in many cases you'd want certain fields to be optional. | 在摆弄了一会之后,你或许会发现管理工具有个限制:编辑表单需要你填写每一个字段,然而在有些情况下,你想要某些字段是可选的。 | 翻译 | ||
| 193#翻译 | Lets say, for example, that we want our <literal>Author</literal> model's <literal>email</literal> field to be optional that is, a blank string should be allowed. | 举个例子,我们想要<literal>Author</literal>模块中的<literal>email</literal>字段成为可选,即允许不填。 | 翻译 | ||
| 194#翻译 | In the real world, you might not have an e-mail address on file for every author. | 在现实世界中,你可能没有为每个作者登记邮箱地址。 | 翻译 | ||
| 196#翻译 | To specify that the <literal>email</literal> field is optional, edit the <literal>Book</literal> model (which, as you'll recall from Chapter 5, lives in <literal>mysite/books/models.py</literal> ). Simply add <literal>blank=True</literal> to the <literal>email</literal> field, like so: | 为了指定<literal>email</literal>字段为可选,你只需编辑<literal>Book</literal>模块(回想第五章,它在<literal>mysite/books/models.py</literal>文件里。),为<literal>email</literal>字段上加上<literal>blank=True</literal>。代码如下: | 翻译 | ||
| 199#翻译 | This tells Django that a blank value is indeed allowed for authors' e-mail addresses. | 这些代码告诉Django,作者的邮箱地址允许输入一个空值。 | 翻译 | ||
| 200#翻译 | By default, all fields have <literal>blank=False</literal> , which means blank values are not allowed. | 所有字段都默认<literal>blank=False</literal>,这使得它们不允许输入空值。 | 翻译 | ||
| 202#翻译 | There's something interesting happening here. | 这里会发生一些有趣的事情。 | 翻译 | ||
| 203#翻译 | Until now, with the exception of the <literal>__unicode__()</literal> method, our models have served as definitions of our database tables Pythonic expressions of SQL <literal>CREATE TABLE</literal> statements, essentially. | 直到现在,除了<literal>__unicode__()</literal>方法,我们的模块充当数据库中表定义的角色,即本质上是用Python的语法来写<literal>CREATE TABLE</literal>语句。 | 翻译 | ||
| 204#翻译 | In adding <literal>blank=True</literal> , we have begun expanding our model beyond a simple definition of what the database table looks like. | 在添加<literal>blank=True</literal>过程中,我们已经开始在简单的定义数据表上扩展我们的模块了。 | 翻译 | ||
| 205#翻译 | Now, our model class is starting to become a richer collection of knowledge about what <literal>Author</literal> objects are and what they can do. | 现在,我们的模块类开始成为一个富含<literal>Author</literal>对象属性和行为的集合了。 | 翻译 | ||
| 206#翻译 | Not only is the <literal>email</literal> field represented by a <literal>VARCHAR</literal> column in the database; it's also an optional field in contexts such as the Django admin site. | <literal>email</literal>不但展现为一个数据库中的<literal>VARCHAR</literal>类型的字段,它还是页面中可选的字段,就像在管理工具中看到的那样。 | 翻译 | ||
| 208#翻译 | Once you've added that <literal>blank=True</literal> , reload the Add author edit form (<literal>http://127.0.0.1:8000/admin/books/author/add/</literal> ), and you'll notice the fields label Email is no longer bolded. | 当你添加<literal>blank=True</literal>以后,刷新页面Add author edit form (<literal>http://127.0.0.1:8000/admin/books/author/add/</literal> ),你会发现Email的标签不再是粗体了。 | 翻译 | ||
| 209#翻译 | This signifies it's not a required field. | 这意味它不是一个必填字段。 | 翻译 | ||
| 210#翻译 | You can now add authors without needing to provide e-mail addresses; you won't get the loud red This field is required message anymore, if the field is submitted empty. | 现在你可以添加一个作者而不必输入邮箱地址,即使你为这个字段提交了一个空值,也再不会得到那刺眼的红色信息“This field is required”。 | 翻译 | ||
| 212#翻译 | Making Date and Numeric Fields Optional | 设置日期型和数字型字段可选 | 翻译 | ||
| 214#翻译 | A common gotcha related to <literal>blank=True</literal> has to do with date and numeric fields, but it requires a fair amount of background explanation. | 虽然<literal>blank=True</literal>同样适用于日期型和数字型字段,但是这里需要详细讲解一些背景知识。 | 翻译 | ||
| 216#翻译 | SQL has its own way of specifying blank values a special value called <literal>NULL</literal> . <literal>NULL</literal> could mean unknown, or invalid, or some other application-specific meaning. | SQL有指定空值的独特方式,它把空值叫做NULL。NULL可以表示为未知的、非法的、或其它程序指定的含义。 | 翻译 | ||
| 218#翻译 | In SQL, a value of <literal>NULL</literal> is different than an empty string, just as the special Python object <literal>None</literal> is different than an empty Python string (<literal>""</literal> ). This means it's possible for a particular character field (e.g., a <literal>VARCHAR</literal> column) to contain both <literal>NULL</literal> values and empty string values. | 在SQL中, <literal>NULL</literal>的值不同于空字符串,就像Python中<literal>None</literal>不同于空字符串(<literal>""</literal>)一样。这意味着某个字符型字段(如<literal>VARCHAR</literal>)的值可能同时包含<literal>NULL</literal>和空字符串。 | 翻译 | ||
| 220#翻译 | This can cause unwanted ambiguity and confusion: | 这会引起不必要的歧义或疑惑。 | 翻译 | ||
| 221#翻译 | Why does this record have a <literal>NULL</literal> but this other one has an empty string? | 为什么这条记录有个<literal>NULL</literal>,而那条记录却有个空字符串? | 翻译 | ||
| 222#翻译 | Is there a difference, or was the data just entered inconsistently? | 它们之间有区别,还是数据输入不一致? | 翻译 | ||
| 223#翻译 | And: | 还有: | 翻译 | ||
| 224#翻译 | How do I get all the records that have a blank value should I look for both <literal>NULL</literal> records and empty strings, or do I only select the ones with empty strings? | 我怎样才能得到全部拥有空值的记录,应该按<literal>NULL</literal>和空字符串查找么?还是仅按字符串查找? | 翻译 | ||
| 226#翻译 | To help avoid such ambiguity, Django's automatically generated <literal>CREATE TABLE</literal> statements (which were covered in Chapter 5) add an explicit <literal>NOT NULL</literal> to each column definition. | 为了消除歧义,Django生成<literal>CREATE TABLE</literal>语句自动为每个字段显式加上<literal>NOT NULL</literal>。 | 翻译 | ||
| 227#翻译 | For example, here's the generated statement for our <literal>Author</literal> model, from Chapter 5: | 这里有个第五章中生成<literal>Author</literal>模块的例子: | 翻译 | ||
| 230#翻译 | In most cases, this default behavior is optimal for your application and will save you from data-inconsistency headaches. | 在大多数情况下,这种默认的行为对你的应用程序来说是最佳的,因为它可以使你不再因数据一致性而头痛。 | 翻译 | ||
| 231#翻译 | And it works nicely with the rest of Django, such as the Django admin site, which inserts an empty string (<emphasis>not</emphasis> a <literal>NULL</literal> value) when you leave a character field blank. | 而且它可以和Django的其它部分工作得很好。如在管理工具中,如果你留空一个字符型字段,它会为此插入一个空字符串(而<emphasis> 不是</emphasis><literal>NULL</literal>)。 | 翻译 | ||
| 233#翻译 | But there's an exception with database column types that do not accept empty strings as valid values such as dates, times and numbers. | 但是,其它数据类型有例外:日期型、时间型和数字型字段不接受空字符串。 | 翻译 | ||
| 234#翻译 | If you try to insert an empty string into a date or integer column, you'll likely get a database error, depending on which database you're using. | 如果你尝试将一个空字符串插入日期型或整数型字段,你可能会得到数据库返回的错误,这取决于那个数据库的类型。 | 翻译 | ||
| 235#翻译 | (PostgreSQL, which is strict, will raise an exception here; MySQL might accept it or might not, depending on the version you're using, the time of day and the phase of the moon.) In this case, <literal>NULL</literal> is the only way to specify an empty value. | (PostgreSQL比较严禁,会抛出一个异常;MySQL可能会也可能不会接受,这取决于你使用的版本和运气了。)在这种情况下,<literal>NULL</literal>是唯一指定空值的方法。 | 翻译 | ||
| 236#翻译 | In Django models, you can specify that <literal>NULL</literal> is allowed by adding <literal>null=True</literal> to a field. | 在Django模块中,你可以通过添加<literal>null=True</literal>来指定一个字段允许为<literal>NULL</literal>。 | 翻译 | ||
| 238#翻译 | So that's a long way of saying this: | 因此,这说起来有点复杂: | 翻译 | ||
| 239#翻译 | if you want to allow blank values in a date field (e.g., <literal>DateField</literal> , <literal>TimeField</literal> , <literal>DateTimeField</literal> ) or numeric field (e.g., <literal>IntegerField</literal> , <literal>DecimalField</literal> , <literal>FloatField</literal> ), you'll need to use both <literal>null=True</literal> | 如果你想允许一个日期型(<literal>DateField</literal>、<literal>TimeField</literal>、<literal>DateTimeField</literal>)或数字型(<literal>IntegerField</literal>、<literal>DecimalField</literal>、<literal>FloatField</literal>)字段为空,你需要同时使用<literal>null=True</literal> | 翻译 | ||
| 240#翻译 | <emphasis>and</emphasis> | <emphasis> 和</emphasis> | 翻译 | ||
| 241#翻译 | <literal>blank=True</literal> . | <literal>blank=True</literal>。 | 翻译 | ||
| 243#翻译 | For sake of example, let's change our <literal>Book</literal> model to allow a blank <literal>publication_date</literal> . Here's the revised code: | 为了举例说明,让我们把<literal>Book</literal>模块修改成允许 <literal>publication_date</literal>为空。修改后的代码如下: | 翻译 | ||
| 246#翻译 | Adding <literal>null=True</literal> is more complicated than adding <literal>blank=True</literal> , because <literal>null=True</literal> changes the semantics of the database that is, it changes the <literal>CREATE TABLE</literal> statement to remove the <literal>NOT NULL</literal> from the <literal>publication_date</literal> field. | 添加<literal>null=True</literal>比添加<literal>blank=True</literal>复杂。因为<literal>null=True</literal>改变了数据的语义,即改变了<literal>CREATE TABLE</literal>语句,把<literal>publication_date</literal>字段上的<literal>NOT NULL</literal>删除了。 | 翻译 | ||
| 247#翻译 | To complete this change, we'll need to update the database. | 要完成这些改动,我们还需要更新数据库。 | 翻译 | ||
| 249#翻译 | For a number of reasons, Django does not attempt to automate changes to database schemas, so it's your own responsibility to execute the appropriate <literal>ALTER TABLE</literal> statement whenever you make such a change to a model. | 出于某种原因,Django不会尝试自动更新数据库结构。所以你必须自已执行<literal>ALTER TABLE</literal>语句将模块的改动更新至数据库。 | 翻译 | ||
| 250#翻译 | Recall that you can use <literal>manage.py dbshell</literal> to enter your database server's shell. | 像先前那样,你可以使用<literal>manage.py dbshell</literal>进入数据库服务环境。 | 翻译 | ||
| 251#翻译 | Here's how to remove the <literal>NOT NULL</literal> in this particular case: | 以下是在这个特殊情况下如何删除<literal>NOT NULL</literal>: | 翻译 | ||
| 254#翻译 | (Note that this SQL syntax is specific to PostgreSQL.) | (注意:此SQL语法是针对PostgreSQL的。) | 翻译 | ||
| 256#翻译 | We'll cover schema changes in more depth in Chapter 10. | 我们将在第十章详细讲述数据库结构更改。 | 翻译 | ||
| 258#翻译 | Bringing this back to the admin site, now the Add book edit form should allow for empty publication date values. | 现在让我们回到管理工具,添加book的编辑页面允许输入一个空的publication date。 | 翻译 | ||
| 260#翻译 | Customizing Field Labels | 自定义字段标签 | 翻译 | ||
| 262#翻译 | On the admin site's edit forms, each field's label is generated from its model field name. | 在编辑页面中,每个字段的标签都是从模块的字段名称生成的。 | 翻译 | ||
| 263#翻译 | The algorithm is simple: | 规则很简单: | 翻译 | ||
| 264#翻译 | Django just replaces underscores with spaces and capitalizes the first character, so, for example, the <literal>Book</literal> model's <literal>publication_date</literal> field has the label Publication date. | 用空格替换下划线;首字母大写。例如:<literal>Book</literal>模块中<literal>publication_date</literal>的标签是Publication date。 | 翻译 | ||
| 266#翻译 | However, field names don't always lend themselves to nice admin field labels, so in some cases you might want to customize a label. | 然而,字段名称并不总是贴切的。有些情况下,你可能想自定义一个标签。 | 翻译 | ||
| 267#翻译 | You can do this by specifying <literal>verbose_name</literal> in the appropriate model field. | 你只需在模块字段中指定<literal>verbose_name</literal>。 | 翻译 | ||
| 269#翻译 | For example, here's how we can change the label of the <literal>Author.email</literal> field to e-mail, with a hyphen: | 举个例子,说明如何将<literal>Author.email</literal>的标签改为e-mail,中间有个横线。 | 翻译 | ||
| 272#翻译 | Make that change and reload the server, and you should see the field's new label on the author edit form. | 修改后重启服务器,你会在author编辑页面中看到这个新标签。 | 翻译 | ||
| 274#翻译 | Note that you shouldn't capitalize the first letter of a <literal>verbose_name</literal> unless it should <emphasis>always</emphasis> be capitalized (e.g., <literal>"USA state"</literal> ). Django will automatically capitalize it when it needs to, and it will use the exact <literal>verbose_name</literal> value in other places that don't require capitalization. | 请注意,你不必把<literal>verbose_name</literal>的首字母大写,除非是连续大写(如:<literal>"USA state"</literal>)。Django会自动适时将首字母大写,并且在其它不需要大写的地方使用<literal>verbose_name</literal>的精确值。 | 翻译 | ||
| 276#翻译 | Finally, note that you can pass the <literal>verbose_name</literal> as a positional argument, for a slightly more compact syntax. | 最后还需注意的是,为了使语法简洁,你可以把它当作固定位置的参数传递。 | 翻译 | ||
| 277#翻译 | This example is equivalent to the previous one: | 这个例子与上面那个的效果相同。 | 翻译 | ||
| 280#翻译 | This won't work with <literal>ManyToManyField</literal> or <literal>ForeignKey</literal> fields, though, because they require the first argument to be a model class. | 但这不适用于<literal>ManyToManyField</literal> 和<literal>ForeignKey</literal>字段,因为它们的第一个参数必须是模块类。 | 翻译 | ||
| 281#翻译 | In those cases, specifying <literal>verbose_name</literal> explicitly is the way to go. | 那种情形,必须显式使用<literal>verbose_name</literal>这个参数名称。 | 翻译 | ||
| 283#翻译 | Custom ModelAdmin classes | 自定义ModelAdmi类 | 翻译 | ||
| 285#翻译 | The changes we've made so far <literal>blank=True</literal> , <literal>null=True</literal> and <literal>verbose_name</literal> are really model-level changes, not admin-level changes. | 迄今为止,我们做的<literal>blank=True</literal>、<literal>null=True</literal>和<literal>verbose_name</literal>修改其实是模块级别,而不是管理级别的。 | 翻译 | ||
| 286#翻译 | That is, these changes are fundamentally a part of the model and just so happen to be used by the admin site; there's nothing admin-specific about them. | 也就是说,这些修改实质上是构成模块的一部分,并且正好被管理工具使用,而不是专门针对管理工具的。 | 翻译 | ||
| 288#翻译 | Beyond these, the Django admin site offers a wealth of options that let you customize how the admin site works for a particular model. | 除了这些,Django还提供了大量选项让你针对特别的模块自定义管理工具。 | 翻译 | ||
| 289#翻译 | Such options live in <emphasis>ModelAdmin classes</emphasis> , which are classes that contain configuration for a specific model in a specific admin site instance. | 这些选项都在<emphasis>ModelAdmin classes</emphasis>里面,这些类包含了管理工具中针对特别模块的配置。 | 翻译 | ||
| 291#翻译 | Customizing change lists | 自定义列表 | 翻译 | ||
| 293#翻译 | Let's dive into admin customization by specifying the fields that are displayed on the change list for our <literal>Author</literal> model. | 让我们更深一步:自定义<literal>Author</literal>模块的列表中的显示字段。 | 翻译 | ||
| 294#翻译 | By default, the change list displays the result of <literal>__unicode__()</literal> for each object. | 列表默认地显示查询结果中对象的<literal>__unicode__()</literal>。 | 翻译 | ||
| 295#翻译 | In Chapter 5, we defined the <literal>__unicode__()</literal> method for <literal>Author</literal> objects to display the first name and last name together: | 在第五章中,我们定义<literal>Author</literal>对象的<literal>__unicode__()</literal>方法,用以同时显示作者的姓和名。 | 翻译 | ||
| 298#翻译 | As a result, the change list for <literal>Author</literal> objects displays each other's first name and last name together, as you can see in Figure 6-7. | 结果正如图6-7所示,列表中显示的是每个作者的姓名。 | 翻译 | ||
| 300#翻译 | <image alt="Screenshot of the author change list page." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/author_changelist1.png"/><cnid>119 | 119 | 翻译 | ||
| 301#翻译 | Figure 6-7. | 图 6-7. | 翻译 | ||
| 302#翻译 | The author change list page | 作者列表 | 翻译 | ||
| 304#翻译 | We can improve on this default behavior by adding a few other fields to the change list display. | 我们可以在这基础上改进,添加其它字段,从而改变列表的显示。 | 翻译 | ||
| 305#翻译 | It'd be handy, for example, to see each author's e-mail address in this list, and it'd be nice to be able to sort by first and last name. | 这个页面应该提供便利,比如说:在这个列表中可以看到作者的邮箱地址。如果能按照姓氏或名字来排序,那就更好了。 | 翻译 | ||
| 307#翻译 | To make this happen, we'll define a <literal>ModelAdmin</literal> class for the <literal>Author</literal> model. | 为了达到这个目的,我们将为<literal>Author</literal>模块定义一个<literal>ModelAdmin</literal>类。 | 翻译 | ||
| 308#翻译 | This class is the key to customizing the admin, and one of the most basic things it lets you do is specify the list of fields to display on change list pages. | 这个类是自定义管理工具的关键,其中最基本的一件事情是允许你指定列表中的显示字段。 | 翻译 | ||
| 309#翻译 | Edit <literal>admin.py</literal> to make these changes: | 打开<literal>admin.py</literal>并修改: | 翻译 | ||
| 312#翻译 | Here's what weve done: | 解释一下代码: | 翻译 | ||
| 314#翻译 | We created the class <literal>AuthorAdmin</literal> . This class, which subclasses <literal>django.contrib.admin.ModelAdmin</literal> , holds custom configuration for a specific admin model. | 我们新建了一个类<literal>AuthorAdmin</literal>,它是从<literal>django.contrib.admin.ModelAdmin</literal>派生出来的子类,保存着一个类的自定义配置,以供管理工具使用。 | 翻译 | ||
| 315#翻译 | We've only specified one customization <literal>list_display</literal> , which is set to a tuple of field names to display on the change list page. | 我们只自定义了一项:<literal>list_display</literal>, 它是一个用于控制列表显示字段的元组。 | 翻译 | ||
| 316#翻译 | These field names must exist in the model, of course. | 当然,这些字段名称必须是模块中存在的。 | 翻译 | ||
| 318#翻译 | We altered the <literal>admin.site.register()</literal> call to add <literal>AuthorAdmin</literal> after <literal>Author</literal> . You can read this as: | 我们修改了<literal>admin.site.register()</literal>调用,在<literal>Author</literal>后面添加了<literal>AuthorAdmin</literal>。你可以这样理解: | 翻译 | ||
| 319#翻译 | Register the <literal>Author</literal> model with the <literal>AuthorAdmin</literal> options. | 用<literal>AuthorAdmin</literal>选项注册<literal>Author</literal>模块。 | 翻译 | ||
| 321#翻译 | The <literal>admin.site.register()</literal> function takes a <literal>ModelAdmin</literal> subclass as an optional second argument. | <literal>admin.site.register()</literal>函数接受一个<literal>ModelAdmin</literal>子类作为第二个参数。 | 翻译 | ||
| 322#翻译 | If you don't specify a second argument (as is the case for <literal>Publisher</literal> and <literal>Book</literal> ), Django will use the default admin options for that model. | 如果你忽略第二个参数,Django将使用默认的选项。<literal>Publisher</literal>和<literal>Book</literal>的注册就属于这种情况。 | 翻译 | ||
| 324#翻译 | With that tweak made, reload the author change list page, and you'll see its now displaying three columns the first name, last name and e-mail address. | 弄好了这个东东,再刷新author列表页面,你会看到列表中有三列:姓氏、名字和邮箱地址。 | 翻译 | ||
| 325#翻译 | In addition, each of those columns is sortable by clicking on the column header. | 另外,点击每个列的列头可以对那列进行排序。 | 翻译 | ||
| 326#翻译 | (See Figure 6-8.) | (参见图 6-8) | 翻译 | ||
| 328#翻译 | <image alt="Screenshot of the author change list page after list_display." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/author_changelist2.png"/><cnid>130 | 130 | 翻译 | ||
| 329#翻译 | Figure 6-8. | 图 6-8. | 翻译 | ||
| 330#翻译 | The author change list page after list_display | 修改后的author列表页面 | 翻译 | ||
| 332#翻译 | Next, let's add a simple search bar. | 接下来,让我们添加一个快速查询栏。 | 翻译 | ||
| 333#翻译 | Add <literal>search_fields</literal> to the <literal>AuthorAdmin</literal> , like so: | 向<literal>AuthorAdmin</literal>追加<literal>search_fields</literal>,如: | 翻译 | ||
| 336#翻译 | Reload the page in your browser, and you should see a search bar at the top. | 刷新浏览器,你会在页面顶端看到一个查询栏。 | 翻译 | ||
| 337#翻译 | (See Figure 6-9.) We've just told the admin change list page to include a search bar that searches against the <literal>first_name</literal> and <literal>last_name</literal> fields. | (见图6-9.)我们刚才所作的修改列表页面,添加了一个根据姓名查询的查询框。 | 翻译 | ||
| 338#翻译 | As a user might expect, this is case-insensitive and searches both fields, so searching for the string <literal>"bar"</literal> would find both an author with the first name Barney and an author with the last name Hobarson. | 正如用户所希望的那样,它是大小写不敏感的,并且对两个字段检索的查询框。如果查询<literal>"bar"</literal>,那么名字中含有Barney和姓氏中含有Hobarson的作者记录将被检索出来。 | 翻译 | ||
| 340#翻译 | <image alt="Screenshot of the author change list page after search_fields." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/author_changelist3.png"/><cnid>136 | 136 | 翻译 | ||
| 341#翻译 | Figure 6-9. | 图 6-9. | 翻译 | ||
| 342#翻译 | The author change list page after search_fields | 含search_fields的author列表页面 | 翻译 | ||
| 344#翻译 | Next, let's add some date filters to our <literal>Book</literal> model's change list page: | 接下来,让我们为<literal>Book</literal>列表页添加一些日期过滤器。 | 翻译 | ||
| 347#翻译 | Here, because we're dealing with a different set of options, we created a separate <literal>ModelAdmin</literal> class <literal>BookAdmin</literal> . First, we defined a <literal>list_display</literal> just to make the change list look a bit nicer. | 由于我们要处理一系列选项,因此我们创建了一个单独的<literal>ModelAdmin</literal>类:<literal>BookAdmin</literal>。首先,我们定义一个<literal>list_display</literal>,以使得页面好看些。 | 翻译 | ||
| 348#翻译 | Then, we used <literal>list_filter</literal> , which is set to a tuple of fields to use to create filters along the right side of the change list page. | 然后,我们用<literal>list_filter</literal>这个字段元组创建过滤器,它位于列表页面的右边。 | 翻译 | ||
| 349#翻译 | For date fields, Django provides shortcuts to filter the list to Today, Past 7 days, This month and This year shortcuts that Django's developers have found hit the common cases for filtering by date. | Django为日期型字段提供了快捷过滤方式,它包含:今天、过往七天、当月和今年。这些是开发人员经常用到的。 | 翻译 | ||
| 350#翻译 | Figure 6-10 shows what that looks like. | 图 6-10显示了修改后的页面。 | 翻译 | ||
| 352#翻译 | <image alt="Screenshot of the book change list page after list_filter." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/book_changelist1.png"/><cnid>142 | 142 | 翻译 | ||
| 353#翻译 | Figure 6-10. | 图 6-10. | 翻译 | ||
| 354#翻译 | The book change list page after list_filter | 含过滤器的book列表页面 | 翻译 | ||
| 356#翻译 | <literal>list_filter</literal> also works on fields of other types, not just <literal>DateField</literal> . (Try it with <literal>BooleanField</literal> and <literal>ForeignKey</literal> fields, for example.) The filters show up as long as there are at least 2 values to choose from. | <literal> 过滤器</literal> 同样适用于其它类型的字段,而不单是<literal> 日期型</literal> (请在<literal> 布尔型</literal> 和<literal> 外键</literal> 字段上试试)。当有两个以上值时,过滤器就会显示。 | 翻译 | ||
| 358#翻译 | Another way to offer date filters is to use the <literal>date_hierarchy</literal> admin option, like this: | 另外一种过滤日期的方式是使用<literal>date_hierarchy</literal>选项,如: | 翻译 | ||
| 361#翻译 | With this in place, the change list page gets a date drill-down navigation bar at the top of the list, as shown in Figure 6-11. | 修改好后,页面中的列表顶端会有一个逐层深入的日期导航条,效果如图 6-11。 | 翻译 | ||
| 362#翻译 | It starts with a list of available years, then drills down into months and individual days. | 它从可用的年份开始,然后逐层细分到月乃至日。 | 翻译 | ||
| 364#翻译 | <image alt="Screenshot of the book change list page after date_hierarchy." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/book_changelist2.png"/><cnid>149 | 149 | 翻译 | ||
| 365#翻译 | Figure 6-11. | 图 6-11. | 翻译 | ||
| 366#翻译 | The book change list page after date_hierarchy | 含date_hierarchy的book列表页面 | 翻译 | ||
| 368#翻译 | Note that <literal>date_hierarchy</literal> takes a <emphasis>string</emphasis> , not a tuple, because only one date field can be used to make the hierarchy. | 请注意,<literal>date_hierarchy</literal>接受的是<emphasis> 字符串</emphasis> ,而不是元组。因为只能对一个日期型字段进行层次划分。 | 翻译 | ||
| 370#翻译 | Finally, let's change the default ordering so that books on the change list page are always ordered descending by their publication date. | 最后,让我们改变默认的排序方式,按publication_date降序排列。 | 翻译 | ||
| 371#翻译 | By default, the change list orders objects according to their model's <literal>ordering</literal> within <literal>class Meta</literal> (which we covered in Chapter 5) but you haven't specified this <literal>ordering</literal> value, then the ordering is undefined. | 列表页面默认按照模块<literal>class Meta</literal>(详见第五章)中的<literal>ordering</literal>所指的列排序。但目前没有指定<literal>ordering</literal>值,所以当前排序是没有定义的。 | 翻译 | ||
| 374#翻译 | This admin <literal>ordering</literal> option works exactly as the <literal>ordering</literal> in models' <literal>class Meta</literal> , except that it only uses the first field name in the list. | 这个<literal>ordering</literal>选项基本像模块中<literal>class Meta</literal>的<literal>ordering</literal>那样工作,除了它只用列表中的第一个字段名。 | 翻译 | ||
| 375#翻译 | Just pass a list or tuple of field names, and add a minus sign to a field to use descending sort order. | 如果要实现降序,仅需在传入的列表或元组的字段前加上一个减号(-)。 | 翻译 | ||
| 377#翻译 | Reload the book change list to see this in action. | 刷新book列表页面观看实际效果。 | 翻译 | ||
| 378#翻译 | Note that the Publication date header now includes a small arrow that indicates which way the records are sorted. | 注意Publication date列头现在有一个小箭头来显示记录已经排序。 | 翻译 | ||
| 379#翻译 | (See Figure 6-12.) | (见图 6-12.) | 翻译 | ||
| 381#翻译 | <image alt="Screenshot of the book change list page after ordering." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/book_changelist3.png"/><cnid>157 | 157 | 翻译 | ||
| 382#翻译 | Figure 6-12. | 图 6-12 | 翻译 | ||
| 383#翻译 | The book change list page after ordering | 含排序的book列表页面 | 翻译 | ||
| 385#翻译 | We've covered the main change list options here. | 我们已经学习了主要的选项。 | 翻译 | ||
| 386#翻译 | Using these options, you can make a very powerful, production-ready data-editing interface with only a few lines of code. | 通过使用它们,你可以仅需几行代码就能创建一个功能强大、随时上线的数据编辑界面。 | 翻译 | ||
| 388#翻译 | Customizing edit forms | 自定义编辑表单 | 翻译 | ||
| 390#翻译 | Just as the change list can be customized, edit forms can be customized in many ways. | 正如自定义列表那样,编辑表单也可以用很多方式自定义。 | 翻译 | ||
| 392#翻译 | First, let's customize the way fields are ordered. | 首先,我们先自定义字段顺序。 | 翻译 | ||
| 393#翻译 | By default, the order of fields in an edit form corresponds to the order they're defined in the model. | 默认地,表单中的字段顺序是与模块中定义是一致的。 | 翻译 | ||
| 394#翻译 | We can change that using the <literal>fields</literal> option in our <literal>ModelAdmin</literal> subclass: | 我们可以通过使用<literal>ModelAdmin</literal>子类中的<literal>fields</literal>选项来改变它: | 翻译 | ||
| 397#翻译 | After this change, the edit form for books will use the given ordering for fields. | 完成之后,编辑表单将按照指定的顺序显示各字段。 | 翻译 | ||
| 398#翻译 | It's slightly more natural to have the authors after the book title. | 它看起来自然多了——作者排在书名之后。 | 翻译 | ||
| 399#翻译 | Of course, the field order should depend on your data-entry workflow. | 字段顺序当然是与数据条目录入顺序有关, | 翻译 | ||
| 400#翻译 | Every form is different. | 每个表单都不一样。 | 翻译 | ||
| 402#翻译 | Another useful thing the <literal>fields</literal> option lets you do is to <emphasis>exclude</emphasis> certain fields from being edited entirely. | 通过fields这个选项,你可以排除一些不想被其他人编辑的字段。 | 翻译 | ||
| 403#翻译 | Just leave out the field(s) you want to exclude. | 只要不选上不想被编辑的字段即可。 | 翻译 | ||
| 404#翻译 | You might use this if your admin users are only trusted to edit a certain segment of your data, or if part of your fields are changed by some outside, automated process. | 当你的admin用户只是被授权可以更改你的某一部分数据时,或者,你的数据被一些外部的程序自动处理而改变了,你就可以使用这个功能。 | 翻译 | ||
| 405#翻译 | For example, in our book database, we could hide the <literal>publication_date</literal> field from being editable: | 例如,在book数据库中,我们可以隐藏<literal>publication_date</literal>,以防止它被编辑。 | 翻译 | ||
| 408#翻译 | As a result, the edit form for books doesn't offer a way to specify the publication date. | 这样,在编辑页面就无法对publication date进行改动。 | 翻译 | ||
| 409#翻译 | This could be useful, say, if you're an editor who prefers that his authors not push back publication dates. | 如果你是一个编辑,不希望作者推迟出版日期的话,这个功能就很有用。 | 翻译 | ||
| 410#翻译 | (This is purely a hypothetical example, of course.) | (当然,这纯粹是一个假设的例子。) | 翻译 | ||
| 412#翻译 | When a user uses this incomplete form to add a new book, Django will simply set the <literal>publication_date</literal> to <literal>None</literal> so make sure that field has <literal>null=True</literal> . | 当一个用户用这个不包含完整信息的表单添加一本新书时,Django会简单地将<literal>publication_date</literal>设置为<literal>None</literal>,以确保这个字段满足<literal>null=True</literal>的条件。 | 翻译 | ||
| 414#翻译 | Another commonly used edit-form customization has to do with many-to-many fields. | 另一个常用的编辑页面自定义是针对多对多字段的。 | 翻译 | ||
| 415#翻译 | As we've seen on the edit form for books, the admin site represents each <literal>ManyToManyField</literal> as a multiple-select boxes, which is the most logical HTML input widget to use but multiple-select boxes can be difficult to use. | 如我们在book编辑页面看到的那样,<literal> 多对多字段</literal> 被展现成多选框。虽然多选框在逻辑上是最适合的HTML控件,但它却不那么好用。 | 翻译 | ||
| 416#翻译 | If you want to select multiple items, you have to hold down the control key, or command on a Mac, to do so. | 如果你想选择多项,你必须还要按下Ctrl键(苹果机是command键)。 | 翻译 | ||
| 417#翻译 | The admin site helpfully inserts a bit of text that explains this, but, still, it gets unwieldy when your field contains hundreds of options. | 虽然管理工具为此添加了注释(help_text),但是当你的字段有几百个选项时,它依然显得笨拙。 | 翻译 | ||
| 419#翻译 | The admin site's solution is <literal>filter_horizontal</literal> . Let's add that to <literal>BookAdmin</literal> and see what it does. | 更好的办法是使用<literal>filter_horizontal</literal>。让我们把它添加到<literal>BookAdmin</literal>中,然后看看它的效果。 | 翻译 | ||
| 422#翻译 | (If you're following along, note that we've also removed the <literal>fields</literal> option to restore all the fields in the edit form.) | (如果你一着跟着做练习,请注意移除<literal>fields</literal>选项,以使得编辑页面包含所有字段。) | 翻译 | ||
| 424#翻译 | Reload the edit form for books, and you'll see that the Authors section now uses a fancy JavaScript filter interface that lets you search through the options dynamically and move specific authors from Available authors to the Chosen authors box, and vice versa. | 刷新book编辑页面,你会看到Author区中有一个精巧的JavaScript过滤器,它允许你动态检索选项,然后将选中的authors从Available框移到Chosen框,还可以移回来。 | 翻译 | ||
| 426#翻译 | <image alt="Screenshot of the book edit form after adding filter_horizontal." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/book_editform1.png"/><cnid>177 | 177 | 翻译 | ||
| 427#翻译 | Figure 6-13. | 图 6-13. | 翻译 | ||
| 428#翻译 | The book edit form after adding filter_horizontal | 含filter_horizontal的book编辑页面 | 翻译 | ||
| 430#翻译 | We'd highly recommend using <literal>filter_horizontal</literal> for any <literal>ManyToManyField</literal> that has more than 10 items. | 我们强烈建议针对那些拥有十个以上选项的<literal> 多对多字段</literal> 使用<literal>filter_horizontal</literal>。 | 翻译 | ||
| 431#翻译 | It's far easier to use than a simple multiple-select widget. | 这比多选框好用多了。 | 翻译 | ||
| 432#翻译 | Also, note you can use <literal>filter_horizontal</literal> for multiple fields just specify each name in the tuple. | 你可以在多个字段上使用<literal>filter_horizontal</literal>,只需在这个元组中指定每个字段的名字。 | 翻译 | ||
| 434#翻译 | <literal>ModelAdmin</literal> classes also support a <literal>filter_vertical</literal> option. | <literal>ModelAdmin</literal>类还支持<literal>filter_vertical</literal>选项。 | 翻译 | ||
| 435#翻译 | This works exactly as <literal>filter_horizontal</literal> , but the resulting JavaScript interface stacks the two boxes vertically instead of horizontally. | 它与<literal>filter_horizontal</literal>非常相似,除了控件都是垂直排列,而不是水平排列的。 | 翻译 | ||
| 436#翻译 | It's a matter of personal taste. | 至于使用哪个,只是个人喜好问题。 | 翻译 | ||
| 438#翻译 | <literal>filter_horizontal</literal> and <literal>filter_vertical</literal> only work on <literal>ManyToManyField</literal> fields, not <literal>ForeignKey</literal> fields. | <literal>filter_horizontal</literal>和<literal>filter_vertical</literal>选项只能用在<literal>多对多字段</literal> 上, 而不能用于 <literal>ForeignKey</literal>字段。 | 翻译 | ||
| 439#翻译 | By default, the admin site uses simple <literal><select></literal> boxes for <literal>ForeignKey</literal> fields, but, as for <literal>ManyToManyField</literal> , sometimes you don't want to incur the overhead of having to select all the related objects to display in the drop-down. | 默认地,管理工具使用<literal> 下拉框</literal> 来展现<literal> 外键</literal> 字段。但是,正如<literal> 多对多字段</literal> 那样,有时候你不想忍受因装载并显示这些选项而产生的大量开销。 | 翻译 | ||
| 440#翻译 | For example, if our book database grows to include thousands of publishers, the Add book form could take a while to load, because it would have to load every publisher for display in the <literal><select></literal> box. | 例如,我们的book数据库膨胀到拥有数千条publishers的记录,以致于book的添加页面装载时间较久,因为它必须把每一个publishe都装载并显示在<literal> 下拉框</literal> 中。 | 翻译 | ||
| 442#翻译 | The way to fix this is to use an option called <literal>raw_id_fields</literal> . Set this to a tuple of <literal>ForeignKey</literal> field names, and those fields will be displayed in the admin with a simple text input box (<literal><input type="text"></literal> ) instead of a <literal><select></literal> . See Figure 6-14. | 解决这个问题的办法是使用<literal> raw_id_fields</literal> 选项。它是一个包含外键字段名称的元组,它包含的字段将被展现成<literal> 文本框</literal> ,而不再是<literal> 下拉框</literal> 。见图 6-14。 | 翻译 | ||
| 445#翻译 | <image alt="Screenshot of edit form after raw_id_fields." uri="http://new-media.djangobook.com/content/en/2.0/chapter06/book_editform2.png"/><cnid>185 | 185 | 翻译 | ||
| 446#翻译 | Figure 6-14. | 图 6-14. | 翻译 | ||
| 447#翻译 | The book edit form after adding raw_id_fields | 含raw_id_fields的book编辑页面 | 翻译 | ||
| 449#翻译 | What do you enter in this input box? | 在这个输入框中,你输入什么呢? | 翻译 | ||
| 450#翻译 | The database ID of the publisher. | publisher的数据库ID号。 | 翻译 | ||
| 451#翻译 | Given that humans don't normally memorize database IDs, there's also a magnifying-glass icon that you can click to pull up a pop-up window, from which you can select the publisher to add. | 考虑到人们通常不会记住这些数据库ID,管理工具提供了一个放大镜图标方便你输入。点击那个图标将会弹出一个窗口,在那里你可以选择想要添加的publisher。 | 翻译 | ||
| 453#翻译 | Users, Groups, and Permissions | 用户、用户组和权限 | 翻译 | ||
| 455#翻译 | Because you're logged in as a superuser, you have access to create, edit, and delete any object. | 因为你是用超级用户登录的,你可以创建,编辑和删除任何对像。 | 翻译 | ||
| 456#翻译 | Naturally, different environments require different permission systems not everybody can or should be a superuser. | 然而,不同的环境要求有不同的权限,系统不允许所有人都是超级用户。 | 翻译 | ||
| 457#翻译 | Django's admin site uses a permissions system that you can use to give specific users access only to the portions of the interface that they need. | 管理工具有一个用户权限系统,通过它你可以根据用户的需要来指定他们的权限,从而达到部分访问系统的目的。 | 翻译 | ||
| 459#翻译 | These user accounts are meant to be generic enough to be used outside of the admin interface, but we'll just treat them as admin user accounts for now. | 用户帐号应该是通用的、独立于管理界面以外仍可以使用。但我们现在把它看作是管理界面的一部分。 | 翻译 | ||
| 460#翻译 | In Chapter 14, we'll cover how to integrate user accounts with the rest of your site (i.e., not just the admin site). | 在第十四章,我们将讲述如何把用户帐号与你的网站(不仅仅是管理工具)集成在一起。 | 翻译 | ||
| 462#翻译 | You can edit users and permissions through the admin interface just like any other object. | 你通过管理界面编辑用户及其权限,就像你编辑别的对象一样。 | 翻译 | ||
| 463#翻译 | We saw this earlier in this chapter, when we played around with the User and Group sections of the admin. | 我们在本章的前面,浏览用户和用户组区域的时候已经见过这些了。 | 翻译 | ||
| 464#翻译 | User objects have the standard username, password, e-mail and real name fields you might expect, along with a set of fields that define what the user is allowed to do in the admin interface. | 如你所想,用户对象有标准的用户名、密码、邮箱地址和真实姓名字段,同时它还拥有关于使用管理界面的权限定义的字段。 | 翻译 | ||
| 465#翻译 | First, there's a set of three boolean flags: | 首先,这有一组三个布尔型标记: | 翻译 | ||
| 467#翻译 | The active flag controls whether the user is active at all. | 激活标志,它用来控制用户是否已经激活。 | 翻译 | ||
| 468#翻译 | If this flag is off and the user tries to log in, he won't be allowed in, even with a valid password. | 如果一个用户帐号的这个标记是关闭状态,而用户又尝试用它登录时,即使密码正确,他也无法登录系统。 | 翻译 | ||
| 470#翻译 | The staff flag controls whether the user is allowed to log in to the admin interface (i.e., whether that user is considered a staff member in your organization). | 成员标志,它用来控制这个用户是否可以登录管理界面(即:这个用户是不是你们组织里的成员)。 | 翻译 | ||
| 471#翻译 | Since this same user system can be used to control access to public (i.e., non-admin) sites (see Chapter 14), this flag differentiates between public users and administrators. | 由于相同的用户可以被用于控制公共页面(即:非管理页面)的存取(详见第十四章),这个标志可用来区分公共用户和管理用户。 | 翻译 | ||
| 473#翻译 | The superuser flag gives the user full access to add, create and delete any item in the admin interface. | 超级用户标志,它赋予用户在管理界面中添加、修改和删除任何项目的权限。 | 翻译 | ||
| 474#翻译 | If a user has this flag set, then all regular permissions (or lack thereof) are ignored for that user. | 如果一个用户帐号有这个标志,那么所有权限设置(即使没有)都会被忽略。 | 翻译 | ||
| 476#翻译 | Normal admin users that is, active, non-superuser staff members are granted admin access through assigned permissions. | 普通用户,即激活的非超级用户的管理用户可以根据一套设定好的权限进入。 | 翻译 | ||
| 477#翻译 | Each object editable through the admin interface (e.g., books, authors, publishers) has three permissions: | 管理界面中每种可编辑的对象(如:books、authors、publishers)都有三种权限: | 翻译 | ||
| 478#翻译 | a <emphasis>create</emphasis> permission, an <emphasis>edit</emphasis> permission and a <emphasis>delete</emphasis> permission. | <emphasis>创建</emphasis>权限, <emphasis>权限</emphasis> 权限和 <emphasis>删除</emphasis> 权限。 | 翻译 | ||
| 479#翻译 | Assigning permissions to a user grants the user access to do what is described by those permissions. | 给一个用户授权许可也就表明该用户可以进行许可描述的操作。 | 翻译 | ||
| 481#翻译 | When you create a user, that user has no permissions, and it's up to you to give the user specific permissions. | 当你创建一个用户时,它没有任何权限,该有什么权限是由你决定的。 | 翻译 | ||
| 482#翻译 | For example, you can give a user permission to add and change publishers, but not permission to delete them. | 例如,你可以给一个用户添加和修改publishers的权限,而不给他删除的权限。 | 翻译 | ||
| 483#翻译 | Note that these permissions are defined per-model, not per-object so they let you say John can make changes to any book, but they don't let you say John can make changes to any book published by Apress. | 请注意,这些权限是定义在模块级别上,而不是对象级别上的。举个例子,你可以让约翰修改任何图书,但是不能让他仅修改由Apress出版的图书。 | 翻译 | ||
| 484#翻译 | The latter functionality, per-object permissions, is a bit more complicated and is outside the scope of this book but is covered in the Django documentation. | 后面这种基于对象级别的权限设置比较复杂,并且超出了本书的覆盖范围,但你可以在Django documentation中寻找答案。 | 翻译 | ||
| 486#翻译 | Note | 注释 | 翻译 | ||
| 488#翻译 | Access to edit users and permissions is also controlled by this permission system. | 权限管理系统也控制编辑用户和权限。 | 翻译 | ||
| 489#翻译 | If you give someone permission to edit users, she will be able to edit her own permissions, which might not be what you want! | 如果你给某人编辑用户的权限,他可以编辑自己的权限,这种能力可能不是你希望的。 | 翻译 | ||
| 490#翻译 | Giving a user permission to edit users is essentially turning a user into a superuser. | 赋予一个用户修改用户的权限,本质上说就是把他变成一个超级用户。 | 翻译 | ||
| 492#翻译 | You can also assign users to groups. | 你也可以给组中分配用户。 | 翻译 | ||
| 493#翻译 | A <emphasis>group</emphasis> is simply a set of permissions to apply to all members of that group. | <emphasis>组</emphasis> 简化了给组中所有成员授予一套权限的动作。 | 翻译 | ||
| 494#翻译 | Groups are useful for granting identical permissions to a subset of users. | 组在给大量用户特定权限的时候很有用。 | 翻译 | ||
| 496#翻译 | When and Why to Use the Admin Interface And When Not to | 何时、为什么使用管理界面?何时又不使用呢? | 翻译 | ||
| 498#翻译 | After having worked through this chapter, you should have a good idea of how to use Django's admin site. | 经过这一章的学习,你应该对Django管理工具有所认识。 | 翻译 | ||
| 499#翻译 | But we want to make a point of covering <emphasis>when</emphasis> and <emphasis>why</emphasis> you might want to use it and when <emphasis>not</emphasis> to use it. | 但是我们需要表明一个观点:<emphasis> 什么时候</emphasis> 、<emphasis> 为什么</emphasis> 用,以及什么时候又<emphasis> 不</emphasis> 用。 | 翻译 | ||
| 501#翻译 | Django's admin site especially shines when nontechnical users need to be able to enter data; that's the purpose behind the feature, after all. | Django的管理界面对非技术用户要输入他们的数据时特别有用;事实上这个特性就是专门为这个 实现的。 | 翻译 | ||
| 502#翻译 | At the newspaper where Django was first developed, development of a typical online feature say, a special report on water quality in the municipal supply would go something like this: | 在Django最开始开发的新闻报道的行业应用中,有一个典型的在线自来水的水质专题报道 应用,它的实现流程是这样的: | 翻译 | ||
| 504#翻译 | The reporter responsible for the project meets with one of the developers and describes the available data. | 负责这个报道的记者和要处理数据的开发者碰头,提供一些数据给开发者。 | 翻译 | ||
| 506#翻译 | The developer designs Django models to fit this data and then opens up the admin site to the reporter. | 开发者围绕这些数据设计模型然后配置一个管理界面给记者。 | 翻译 | ||
| 508#翻译 | The reporter inspects the admin site to point out any missing or extraneous fields better now than later. | 记者检查管理界面,尽早指出缺少或多余的字段。 | 翻译 | ||
| 509#翻译 | The developer changes the models iteratively. | 开发者来回地修改模块。 | 翻译 | ||
| 511#翻译 | When the models are agreed upon, the reporter begins entering data using the admin site. | 当模块认可后,记者就开始用管理界面输入数据。 | 翻译 | ||
| 512#翻译 | At the same time, the programmer can focus on developing the publicly accessible views/templates (the fun part!). | 同时,程序员可以专注于开发公众访问视图和模板(有趣的部分)。 | 翻译 | ||
| 514#翻译 | In other words, the raison d'tre of Djangos admin interface is facilitating the simultaneous work of content producers and programmers. | 换句话说,Django的管理界面为内容输入人员和开发人员协同工作提供了便利。 | 翻译 | ||
| 516#翻译 | However, beyond these obvious data entry tasks, the admin site is useful in a few other cases: | 当然,除了数据输入方面,我们发现管理界面在下面这些情景中也是很有用的: | 翻译 | ||
| 518#翻译 | <emphasis>Inspecting data models</emphasis> : Once you've defined a few models, it can be quite useful to call them up in the admin interface and enter some dummy data. | <emphasis> 检查模块</emphasis> :当你定义好了若干个模块,在管理页面中把他们调出来然后输入一些虚假的数据,这是相当有用的。 | 翻译 | ||
| 519#翻译 | In some cases, this might reveal data-modeling mistakes or other problems with your models. | 有时候,它能显示数据建模的错误或者模块中其它问题。 | 翻译 | ||
| 521#翻译 | <emphasis>Managing acquired data</emphasis> : For applications that rely on data coming from external sources (e.g., users or Web crawlers), the admin site gives you an easy way to inspect or edit this data. | <emphasis> 管理既得数据</emphasis> :如果你的应用程序依赖外部数据(来自用户输入或网络爬虫),管理界面提供了一个便捷的途径,让你检查和编辑那些数据。 | 翻译 | ||
| 522#翻译 | You can think of it as a less powerful, but more convenient, version of your database's command-line utility. | 你可以把它看作是一个功能不那么强大,但是很方便的数据库命令行工具。 | 翻译 | ||
| 524#翻译 | <emphasis>Quick and dirty data-management apps</emphasis> : You can use the admin site to build yourself a very lightweight data management app say, to keep track of expenses. | <emphasis> 临时的数据管理程序</emphasis> :你可以用管理工具建立自己的轻量级数据管理程序,比如说开销记录。 | 翻译 | ||
| 525#翻译 | If you're just building something for your own needs, not for public consumption, the admin site can take you a long way. | 如果你正在根据自己的,而不是公众的需要开发些什么,那么管理界面可以带给你很大的帮助。 | 翻译 | ||
| 526#翻译 | In this sense, you can think of it as a beefed up, relational version of a spreadsheet. | 从这个意义上讲,你可以把它看作是一个增强的关系型电子表格。 | 翻译 | ||
| 528#翻译 | One final point we want to make clear is: | 最后一点要澄清的是: | 翻译 | ||
| 529#翻译 | the admin site is not an end-all-be-all. | 管理界面不是终结者。 | 翻译 | ||
| 530#翻译 | Over the years, we've seen it hacked and chopped up to serve a variety of functions it wasn't intended to serve. | 过往许多年间,我们看到它被拆分、修改成若干个功能模块,而这些功能不是它所支持的。 | 翻译 | ||
| 531#翻译 | Its not intended to be a <emphasis>public</emphasis> interface to data, nor is it intended to allow for sophisticated sorting and searching of your data. | 它不应成为一个<emphasis> 公众</emphasis> 数据访问接口,也不应允许对你的数据进行复杂的排序和查询。 | 翻译 | ||
| 532#翻译 | As we said early in this chapter, it's for trusted site administrators. | 正如本章开头所说,它仅提供给可信任的管理员。 | 翻译 | ||
| 533#翻译 | Keeping this sweet spot in mind is the key to effective admin-site usage. | 请记住这一点,它是有效使用管理界面的钥匙。 | 翻译 | ||
| 535#翻译 | What's Next? | 下一章 | 翻译 | ||
| 537#翻译 | So far we've created a few models and configured a top-notch interface for editing data. | 到现在,我们已经创建了一些模型,并且为编辑数据配置了一个优秀的界面。 | 翻译 | ||
| 538#翻译 | In the <reference name="next chapter" refuri="../chapter07/">next chapter</reference>, we'll move on to the real meat and potatoes of Web development: | <reference name="next chapter" refuri="../chapter07/"> 下一章</reference> ,我们将转入到网站开发中最重要的部分: | 翻译 | ||
| 539#翻译 | form creation and processing. | 表单的创建和处理。 | 翻译 |