ID English原文 中文翻译 最近翻译记录 状态 操作
Chapter 16: Integrating with Legacy Databases and Applications
第十六章 集成已有的数据库和应用
1811天前 翻译
Django is best suited for so-called green-field development that is, starting
projects from scratch, as if you were constructing a building on a fresh field
of green grass. But despite the fact that Django favors from-scratch projects,
its possible to integrate the framework into legacy databases and applications.
This chapter explains a few integration strategies.
1739天前 翻译
Integrating with a Legacy Database
261天前 翻译
Djangos database layer generates SQL schemas from Python code but with a legacy
database, you already have the SQL schemas. In such a case, youll need to
create models for your existing database tables. For this purpose, Django comes
with a tool that can generate model code by reading your database table
layouts. This tool is called ``inspectdb`` , and you can call it by executing
the command `` inspectdb`` .
Django的数据库层从Python代码生成SQL schemas--但是对于遗留数据库,你已经拥有SQL schemas,这种情况下你需要为你
该辅助工具称为 inspectdb 
261天前 翻译
Using ``inspectdb``
使用 ``inspectdb``
262天前 翻译
The ``inspectdb`` utility introspects the database pointed to by your settings
file, determines a Django model representation for each of your tables, and
prints the Python model code to standard output.
The ``inspectdb`` 工具内省检查你的配置文件(setting file)指向的数据库,针对你的每一个表生成一个Django model的表现,然后将这些Python model的代码显示在系统的标准输出里面。
261天前 翻译
Heres a walk-through of a typical legacy database integration process from
scratch. The only assumptions are that Django is installed and that you have a
legacy database.
1804天前 翻译
    Create a Django project by running `` startproject mysite``
    (where ``mysite`` is your projects name). Well use ``mysite`` as the
    project name in this example.
通过运行 startproject mysite (这里 ``mysite`` 是你的项目的名字)建立一个Django项目。好的,那我们在这个例子中就用这个 ``mysite`` 作为项目的名字。
1780天前 翻译
    Edit the settings file in that project, ``mysite/`` , to tell
    Django what your database connection parameters are and what the name of
    the database is. Specifically, provide the ``DATABASE_NAME`` ,
    ``DATABASE_HOST`` , and ``DATABASE_PORT`` settings. (Note that some of
    these settings are optional. Refer to Chapter 5 for more information.)
    编辑项目中的配置文件, ``mysite/`` ,告诉Django你的数据库连接参数和数据库名。具体的说,要提供 ``DATABASE_NAME`` ,
    ``DATABASE_HOST`` , 和 ``DATABASE_PORT`` 这些配置信息. (注意,这里面有些配置项是可选的,更多信息参考第五章)
261天前 翻译
    Create a Django application within your project by running ``python
    mysite/ startapp myapp`` (where ``myapp`` is your applications
    name). Well use ``myapp`` as the application name here.
    通过运行 ``python mysite/ startapp myapp`` (这里 ``myapp`` 是你的应用的名字)创建一个Django应用.那么,我们就以 ``myapp`` 做为这个应用的名字.
261天前 翻译
    Run the command ``python mysite/ inspectdb`` . This will examine
    the tables in the ``DATABASE_NAME`` database and print the generated model
    class for each table. Take a look at the output to get an idea of what
    ``inspectdb`` can do.
Mgi4P7  <a href="">egawnqpydxgr</a>, [url=]pqxrfbeebwlx[/url], [link=]ayerpdvfcibx[/link],
2134天前 翻译
    Save the output to the ```` file within your application by using
    standard shell output redirection:
    将标准shell的输出重定向,保存输出到你的应用的 ```` 文件里:
261天前 翻译
    Edit the ``mysite/myapp/`` file to clean up the generated models
    and make any necessary customizations. Well give some hints for this in the
    next section.
    编辑 ``mysite/myapp/`` 文件以清理生成的 models 以及一些必要的定制化。
261天前 翻译
Cleaning Up Generated Models
BJuyYO  <a href="">gudqbthrquuw</a>, [url=]nnferpaaqtza[/url], [link=]pkxlmlxjxtow[/link],
2144天前 翻译
As you might expect, the database introspection isnt perfect, and youll need to
do some light cleanup of the resulting model code. Here are a few pointers for
dealing with the generated models:
这里提醒一点关于处理生成 models 的要点:
3360天前 翻译
    Each database table is converted to a model class (i.e., there is a
    one-to-one mapping between database tables and model classes). This means
    that youll need to refactor the models for any many-to-many join tables
    into ``ManyToManyField`` objects.
dMtfgT  <a href="">ofpjyxnfjlnj</a>, [url=]dpixdvamofrx[/url], [link=]ivaqvrambdds[/link],
2143天前 翻译
    Each generated model has an attribute for every field, including ``id``
    primary key fields. However, recall that Django automatically adds an
    ``id`` primary key field if a model doesnt have a primary key. Thus, youll
    want to remove any lines that look like this:
3356天前 翻译
    Not only are these lines redundant, but also they can cause problems if
    your application will be adding *new* records to these tables. The
    ``inspectdb`` command cannot detect whether a field is autoincremented, so
    its up to you to change this to ``AutoField`` , if necessary.
261天前 翻译
    Each fields type (e.g., ``CharField`` , ``DateField`` ) is determined by
    looking at the database column type (e.g., ``VARCHAR`` , ``DATE`` ). If
    ``inspectdb`` cannot map a columns type to a model field type, it will use
    ``TextField`` and will insert the Python comment ``'This field type is a
    guess.'`` next to the field in the generated model. Keep an eye out for
    that, and change the field type accordingly if needed.
每一个字段类型,如CharField、DateField, 是通过查找数据库列类型如VARCHAR,DATE来确定的。如果inspectdb无法对某个model字段类型根据数据库列类型进行映射,那么它会使用TextField字段进行代替,并且会在所生成model字段后面加入Python注释“该字段类型是猜的”。因此,请特别注意这一点,并且在必要的时候相应的修改这些字段类型。
1806天前 翻译
    If a field in your database has no good Django equivalent, you can safely
    leave it off. The Django model layer is not required to include every field
    in your table(s).
3356天前 翻译
    If a database column name is a Python reserved word (such as ``pass`` ,
    ``class`` , or ``for`` ), ``inspectdb`` will append ``'_field'`` to the
    attribute name and set the ``db_column`` attribute to the real field name
    (e.g., ``pass`` , ``class`` , or ``for`` ).
261天前 翻译
    For example, if a table has an ``INT`` column called ``for`` , the
    generated model will have a field like this:
261天前 翻译
    ``inspectdb`` will insert the Python comment ``'Field renamed because it
    was a Python reserved word.'`` next to the field.
jBI57S  <a href="">jillralfgjok</a>, [url=]eofkzwzlxabw[/url], [link=]zbzzidjvvvxl[/link],
2135天前 翻译
    If your database contains tables that refer to other tables (as most
    databases do), you might need to rearrange the order of the generated
    models so that models that refer to other models are ordered properly. For
    example, if model ``Book`` has a ``ForeignKey`` to model ``Author`` , model
    ``Author`` should be defined before model ``Book`` . If you need to create
    a relationship on a model that has not yet been defined, you can use the
    name of the model, rather than the model object itself.
如果数据库中某张表引用了其他表(正如大多数数据库系统所做的那样),你需要适当的修改所生成model的顺序,以使得这种引用能够正确映射。例如,model Book拥有一个针对于model Author的外键,那么后者应该先于前者被定义。如果你需要为一个还没有被定义的model创建一个关系,那么你可以使用该model的名字,而不是model对象本身。
1807天前 翻译
    ``inspectdb`` detects primary keys for PostgreSQL, MySQL, and SQLite. That
    is, it inserts ``primary_key=True`` where appropriate. For other databases,
    youll need to insert ``primary_key=True`` for at least one field in each
    model, because Django models are required to have a ``primary_key=True``
262天前 翻译
    Foreign-key detection only works with PostgreSQL and with certain types of
    MySQL tables. In other cases, foreign-key fields will be generated as
    ``IntegerField``s, assuming the foreign-key column was an ``INT`` column.
261天前 翻译
Integrating with an Authentication System
262天前 翻译
Its possible to integrate Django with an existing authentication system another
source of usernames and passwords or authentication methods.
261天前 翻译
For example, your company may already have an LDAP setup that stores a username
and password for every employee. It would be a hassle for both the network
administrator and the users themselves if users had separate accounts in LDAP
and the Django-based applications.
3356天前 翻译
To handle situations like this, the Django authentication system lets you plug
in other authentication sources. You can override Djangos default
database-based scheme, or you can use the default system in tandem with other
261天前 翻译
Specifying Authentication Back-ends
261天前 翻译
Behind the scenes, Django maintains a list of authentication back-ends that it
checks for authentication. When somebody calls
``django.contrib.auth.authenticate()`` (as described in Chapter 12), Django
tries authenticating across all of its authentication back-ends. If the first
authentication method fails, Django tries the second one, and so on, until all
back-ends have been attempted.
在后台,Django维护了一个用于检查认证的后台列表。当某个人调用 ``django.contrib.auth.authenticate()`` (如12章中所述)时,Django会尝试对其认证后台进行遍历认证。如果第一个认证方法失败,Django会尝试认证第二个,以此类推,一直到尝试完。
1807天前 翻译
The list of authentication back-ends to use is specified in the
``AUTHENTICATION_BACKENDS`` setting. This should be a tuple of Python path
names that point to Python classes that know how to authenticate. These classes
can be anywhere on your Python path.
1308天前 翻译
By default, ``AUTHENTICATION_BACKENDS`` is set to the following:
261天前 翻译
Thats the basic authentication scheme that checks the Django users database.
3356天前 翻译
The order of ``AUTHENTICATION_BACKENDS`` matters, so if the same username and
password are valid in multiple back-ends, Django will stop processing at the
first positive match.
261天前 翻译
Writing an Authentication Back-end
3356天前 翻译
An authentication back-end is a class that implements two methods:
``get_user(id)`` and ``authenticate(**credentials)`` .
一个认证后台其实就是一个实现了如下两个方法的类: ``get_user(id)`` 和 ``authenticate(**credentials)`` 。
261天前 翻译
The ``get_user`` method takes an ``id`` which could be a username, database ID,
or whatever and returns a ``User`` object.
方法 ``get_user`` 需要一个参数 ``id`` ,这个 ``id`` 可以是用户名,数据库ID或者其他任何数值,该方法会返回一个 ``User`` 对象。
1384天前 翻译
The ``authenticate`` method takes credentials as keyword arguments. Most of the
time it looks like this:
方法 ``authenticate`` 使用证书作为关键参数。大多数情况下,该方法看起来如下:
1769天前 翻译
But it could also authenticate a token, like so:
1784天前 翻译
Either way, ``authenticate`` should check the credentials it gets, and it
should return a ``User`` object that matches those credentials, if the
credentials are valid. If theyre not valid, it should return ``None`` .
每一个方法中, ``authenticate`` 都应该检测它所获取的证书,并且当证书有效时,返回一个匹配于该证书的 ``User`` 对象,如果证书无效那么返回 ``None`` 。
3346天前 翻译
The Django admin system is tightly coupled to Djangos own database-backed
``User`` object described in Chapter 12. The best way to deal with this is to
create a Django ``User`` object for each user that exists for your back-end
(e.g., in your LDAP directory, your external SQL database, etc.). Either you
can write a script to do this in advance or your ``authenticate`` method can do
it the first time a user logs in.
如12章中所述,Django管理系统紧密连接于其自己后台数据库的 ``User`` 对象。实现这个功能的最好办法就是为您的后台数据库(如LDAP目录,外部SQL数据库等)中的每个用户都创建一个对应的Django User对象。您可以提前写一个脚本来完成这个工作,也可以在某个用户第一次登陆的时候在 ``authenticate`` 方法中进行实现。
243天前 cergme 翻译
Heres an example back-end that authenticates against a username and password
variable defined in your ```` file and creates a Django ``User``
object the first time a user authenticates:
以下是一个示例后台程序,该后台用于认证定义在 ```` 文件中的username和password变量,并且在该用户第一次认证的时候创建一个相应的Django ``User`` 对象。
261天前 翻译
Integrating with Legacy Web Applications
261天前 翻译
Its possible to run a Django application on the same Web server as an
application powered by another technology. The most straightforward way of
doing this is to use Apaches configuration file, ``httpd.conf`` , to delegate
different URL patterns to different technologies. (Note that Chapter 20 covers
Django deployment on Apache/mod_python, so it might be worth reading that
chapter first before attempting this integration.)
261天前 翻译
The key is that Django will be activated for a particular URL pattern only if
your ``httpd.conf`` file says so. The default deployment explained in Chapter
20 assumes you want Django to power every page on a particular domain:
3356天前 翻译
Here, the ``<Location "/">`` line means handle every URL, starting at the root,
with Django.
这里,  ``<Location "/">`` 这一行表示用Django处理每个以根开头的URL.
261天前 翻译
Its perfectly fine to limit this ``<Location>`` directive to a certain
directory tree. For example, say you have a legacy PHP application that powers
most pages on a domain and you want to install a Django admin site at
``/admin/`` without disrupting the PHP code. To do this, just set the
``<Location>`` directive to ``/admin/`` :
3356天前 翻译
With this in place, only the URLs that start with ``/admin/`` will activate
Django. Any other page will use whatever infrastructure already existed.
KshJiK  <a href="">hychpznxvvpz</a>, [url=]idhrtvjehost[/url], [link=]yssfucouapzc[/link],
2135天前 翻译
Note that attaching Django to a qualified URL (such as ``/admin/`` in this
sections example) does not affect the Django URL parsing. Django works with the
absolute URL (e.g., ``/admin/people/person/add/`` ), not a stripped version of
the URL (e.g., ``/people/person/add/`` ). This means that your root URLconf
should include the leading ``/admin/`` .
请注意,把Diango绑定到的合格的URL(比如在本章例子中的 ``/admin/`` )并不会影响其对URL的解析。绝对路径对Django才是有效的(例如 ``/admin/people/person/add/`` ),而非截断后的URL(例如 ``/people/person/add/`` )。这意味着你的根URLconf必须包含前缀 ``/admin/`` 。
261天前 翻译
Whats Next?
aoRVhS  <a href="">rxhmltxgkdkj</a>, [url=]wmhujqrawkcu[/url], [link=]blebolfmttir[/link],
2135天前 翻译
Speaking of the Django admin site and bending the framework to fit legacy
needs, another common task is to customize the Django admin site. The next
chapter focuses on such customization.
261天前 翻译