Skip to content

Commit 6e4ee65

Browse files
committed
worked out topics/db/managers.txt, ref/databases.txt.
1 parent 362d846 commit 6e4ee65

File tree

2 files changed

+107
-118
lines changed

2 files changed

+107
-118
lines changed

ref/databases.txt

Lines changed: 82 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -119,64 +119,61 @@ Django から利用できる MySQL の全ての機能を使うには、 バー
119119
.. _create your database: http://dev.mysql.com/doc/refman/5.0/en/create-database.html
120120
.. _データベースを作成: `create your database`_
121121

122-
.. TBD
123-
124122
.. _mysql-collation:
125123

126-
Collation settings
127-
~~~~~~~~~~~~~~~~~~
124+
コレーションに関する設定
125+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
128126

129-
The collation setting for a column controls the order in which data is sorted
130-
as well as what strings compare as equal. It can be set on a database-wide
131-
level and also per-table and per-column. This is `documented thoroughly`_ in
132-
the MySQL documentation. In all cases, you set the collation by directly
133-
manipulating the database tables; Django doesn't provide a way to set this on
134-
the model definition.
127+
カラムのコレーション設定は、データの保存方法や、文字列の等価性の定義を制御
128+
しています。コレーションはデータベース全体でも、テーブル単位でも、カラム単
129+
位でも設定できます。コレーションの詳細は MySQL のドキュメントで
130+
`詳しく解説されています <documented thoroughly>`_ 。いずれの場合でも、コレー
131+
ションの設定は直接データベーステーブルに対して行ってください。 Django はモ
132+
デル定義でコレーションを設定する方法を提供していません。
135133

136134
.. _documented thoroughly: http://dev.mysql.com/doc/refman/5.0/en/charset.html
137135

138-
By default, with a UTF-8 database, MySQL will use the
139-
``utf8_general_ci_swedish`` collation. This results in all string equality
140-
comparisons being done in a *case-insensitive* manner. That is, ``"Fred"`` and
141-
``"freD"`` are considered equal at the database level. If you have a unique
142-
constraint on a field, it would be illegal to try to insert both ``"aa"`` and
143-
``"AA"`` into the same column, since they compare as equal (and, hence,
144-
non-unique) with the default collation.
145-
146-
In many cases, this default will not be a problem. However, if you really want
147-
case-sensitive comparisons on a particular column or table, you would change
148-
the column or table to use the ``utf8_bin`` collation. The main thing to be
149-
aware of in this case is that if you are using MySQLdb 1.2.2, the database backend in Django will then return
150-
bytestrings (instead of unicode strings) for any character fields it returns
151-
receive from the database. This is a strong variation from Django's normal
152-
practice of *always* returning unicode strings. It is up to you, the
153-
developer, to handle the fact that you will receive bytestrings if you
154-
configure your table(s) to use ``utf8_bin`` collation. Django itself should work
155-
smoothly with such columns, but if your code must be prepared to call
156-
``django.utils.encoding.smart_unicode()`` at times if it really wants to work
157-
with consistent data -- Django will not do this for you (the database backend
158-
layer and the model population layer are separated internally so the database
159-
layer doesn't know it needs to make this conversion in this one particular
160-
case).
161-
162-
If you're using MySQLdb 1.2.1p2, Django's standard
163-
:class:`~django.db.models.CharField` class will return unicode strings even
164-
with ``utf8_bin`` collation. However, :class:`~django.db.models.TextField`
165-
fields will be returned as an ``array.array`` instance (from Python's standard
166-
``array`` module). There isn't a lot Django can do about that, since, again,
167-
the information needed to make the necessary conversions isn't available when
168-
the data is read in from the database. This problem was `fixed in MySQLdb
169-
1.2.2`_, so if you want to use :class:`~django.db.models.TextField` with
170-
``utf8_bin`` collation, upgrading to version 1.2.2 and then dealing with the
171-
bytestrings (which shouldn't be too difficult) is the recommended solution.
172-
173-
Should you decide to use ``utf8_bin`` collation for some of your tables with
174-
MySQLdb 1.2.1p2, you should still use ``utf8_collation_ci_swedish`` (the
175-
default) collation for the :class:`django.contrib.sessions.models.Session`
176-
table (usually called ``django_session`` and the table
177-
:class:`django.contrib.admin.models.LogEntry` table (usually called
178-
``django_admin_log``). Those are the two standard tables that use
179-
:class:`~django.db.model.TextField` internally.
136+
デフォルトの構成では、 MySQL は UTF-8 のデータベースに対して
137+
``utf8_general_ci_swedish`` コレーションを使います。この設定では、全ての文
138+
字列の等値比較が *大小文字を区別* せず行われます。つまり、 ``"Fred"`` と
139+
``"freD"`` はデータベースレベルでは同じ値だとみなされるのです。そのため、デ
140+
フォルトのコレーションを使っていると、フィールドに ``unique`` 制約をかけた
141+
ときに、 ``"aa"`` と ``"AA"`` は等しいとみなされ (一意性が破れるので) 同じ
142+
カラムに入れられなくなります。
143+
144+
大抵のケースでは、デフォルトの設定はさして問題を起こしません。しかし、特定
145+
のカラムやテーブルで大小文字を区別させたいなら、そのカラムやテーブルに
146+
``utf8_bin`` コレーションを指定せねばなりません。その場合、注意すべきなのは、
147+
MySQLdb 1.2.2 を使っていると、 Django のデータベースバックエンドが、データ
148+
ベースから取り出した文字列フィールドの値として (unicode 文字列ではなく)
149+
bytestring を返すということです。このふるまいは、 *常に* unicode を返す、と
150+
いう Django の通常のやりかたから大きくかけ離れています。コレーションを
151+
``utf8_bin`` にして、 bytestring を受け取ったときの扱いは、開発者に委ねられ
152+
ています。 Django 自体はこのカラムを問題なく扱えますが、一貫性をもってデー
153+
タを処理したければ、 ``django.utils.encoding.smart_unicode()`` を何度も
154+
呼び出すことになるでしょう。(データベースバックエンドレイヤとモデルの操作レ
155+
イヤは内部的に分離しているため、変換が必要かどうかをデータベースレイヤでは
156+
判断できないので) Django はこの変換に関知しないのです。
157+
158+
MySQLdb 1.2.1p2 を使っているなら、コレーションを ``utf8_bin`` にしても、
159+
:class:`~django.db.models.CharField` は unicode 文字列を返します。しかし、
160+
今度は :class:`~django.db.models.TextField` が (Python 標準モジュール
161+
``array`` の) ``array.array`` を返します。データをデータベースから読み出す
162+
ときに、変換に必要な情報が手にはいらないので、 Django 側ではどうしようもあ
163+
りません。
164+
この問題は `MySQLdb 1.2.2 で解決済み <fixed in MySQLdb 1.2.2>`_ なので、
165+
``itf8_bin`` コレーションで :class:`~django.db.models.TextField` を使いたけ
166+
れば、バージョンを 1.2.2 に上げて、バイト文字列として扱うよう勧めます (それ
167+
ほど難しくはありません)。
168+
169+
MySQLdb 1.2.1p2 で ``utf8_bin`` コレーションの設定されたテーブルを使うのな
170+
ら、 :class:`django.contrib.sessions.models.Session` のテーブル
171+
(通常は ``django_session``) や、
172+
:class:`django.contrib.admin.models.LogEntry` のテーブル
173+
(通常は ``django_admin_log``) のコレーションに
174+
``utf8_collation_ci_swedish`` (デフォルトのコレーション) を使わねばなりませ
175+
ん。これらは標準の Django のテーブルのうち、内部的に
176+
:class:`~django.db.model.TextField` を使っているものだからです。
180177

181178
.. _fixed in MySQLdb 1.2.2: http://sourceforge.net/tracker/index.php?func=detail&aid=1495765&group_id=22307&atid=374932
182179

@@ -263,51 +260,47 @@ Django はスキーマを作成する際にストレージエンジンを指定
263260

264261
.. _AlterModelOnSyncDB: http://code.djangoproject.com/wiki/AlterModelOnSyncDB
265262

266-
.. TBD
267-
268-
Boolean fields in Django
269-
-------------------------
263+
``BooleanField`` に関する注意
264+
-------------------------------
270265

271-
Since MySQL doesn't have a direct ``BOOLEAN`` column type, Django uses a
272-
``TINYINT`` column with values of ``1`` and ``0`` to store values for the
273-
:class:`~django.db.models.BooleanField` model field. Refer to the documentation
274-
of that field for more details, but usually this won't be something that will
275-
matter unless you're printing out the field values and are expecting to see
276-
``True`` and ``False.``.
266+
MySQL には直接的な ``BOOLEAN`` カラム型がないので、 Django は
267+
:class:`~django.db.models.BooleanField` の値を ``TINYINT`` カラムを使って 0
268+
または 1 で保存します。詳しくはモデルフィールドのドキュメントを参照してくだ
269+
さい。ただし、フィールドの値を出力したり、値が ``True`` や ``False`` でなけ
270+
ればならないような場合を除いて、特に問題はありません。
277271

278272
.. _sqlite-notes:
279273

280-
SQLite notes
281-
============
274+
SQLite に関する注意
275+
=====================
282276

283-
Versions of SQLite 3.3.5 and older `contain a bug`_ when handling ``ORDER BY``
284-
parameters. This can cause problems when you use the ``select`` parameter for
285-
the ``extra()`` QuerySet method. The bug can be identified by the error message
286-
``OperationalError: ORDER BY terms must not be non-integer constants``. The
287-
problem can be solved updating SQLite to version 3.3.6 or newer, possibly also
288-
updating the ``pysqlite2`` Python module in the process.
277+
バージョン 3.3.5 以前の SQLite には、 ``ORDER_BY`` パラメタの扱いかたに
278+
`バグがあります <contain a bug>`_ 。そのため、クエリセットメソッド
279+
``extra()`` を使って ``select`` のパラメタを指定しようとすると問題を引き起
280+
こします。このバグは、
281+
``OperationalError: ORDER BY terms must not be non-integer constants`` とい
282+
うメッセージを出力します。 SQLite のバージョンを 3.3.6 に上げ、
283+
``pysqlite2`` モジュールを更新すれば、問題を解決できます。
289284

290285
.. _contain a bug: http://www.sqlite.org/cvstrac/tktview?tn=1768
286+
287+
バージョン 3.3.6 は 2006 年の 4 月にリリースされており、現在手に入る各プラッ
288+
トフォームのほとんどのバイナリ配布物で、Python から ``pysqlite2`` や
289+
``sqlite3`` を介して使える SQLite は新しいバージョンなので、あまり大きなイ
290+
ンパクトはありません。
291+
292+
ただし、 Windows の場合、 Python 2.5 の公式の安定版リリースのバイナリ配布物
293+
には (今のところ 2.5.2 も)、 SQLite 3.3.4 が組み込まれているため、バグが確
294+
認されています。このため (Django 1.0 で) 3 つのテストスイートが失敗します。
295+
この問題は、上で述べたように、 新しいバージョン SQLite が組み込まれた
296+
``pysqlite2`` (``pysqlite-2.x.x.win32-py2.5.exe``) をダウンロードしてインス
297+
トールすれば解決できます。 Python 2.6 には新たなバージョンの SQLite が付属
298+
する予定なので、この問題の影響を受けないはずです。
291299

292-
This has a very low impact because 3.3.6 was released in April 2006, so most
293-
current binary distributions for different platforms include newer version of
294-
SQLite usable from Python through either the ``pysqlite2`` or the ``sqlite3``
295-
modules.
296-
297-
However, in the case of Windows, the official binary distribution of the stable
298-
release of Python 2.5 (2.5.2 as of now) includes SQLite 3.3.4 so the bug can
299-
make itself evident in that platform. There are (as of Django 1.0) even three
300-
tests in the Django test suite that will fail when run under this setup. As
301-
described above, this can be solved by downloading and installing a newer
302-
version of ``pysqlite2`` (``pysqlite-2.x.x.win32-py2.5.exe``) that includes and
303-
uses a newer version of SQLite. Python 2.6 will ship with a newer version of
304-
SQLite and so will no be affected by this issue.
305-
306-
If you are in such platform and find yourself in the need to update
307-
``pysqlite``/SQLite, you will also need to manually modify the
308-
``django/db/backends/sqlite3/base.py`` file in the Django source tree so it
309-
attempts to import ``pysqlite2`` before that ``sqlite3`` and so it can take
310-
advantage of the new ``pysqlite2``/SQLite versions.
300+
上記のプラットフォームを使っていて、 ``pysqlite`` や SQLite を更新する必要
301+
があるなら、Django のソースツリーの ``django/db/backends/sqlite3/base.py``
302+
ファイルを手で編集して、 ``sqlite3`` より ``pysqlite2`` が先に import され
303+
るように変更してください。
311304

312305
.. _oracle-notes:
313306
.. _Oracle notes:

topics/db/managers.txt

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -205,33 +205,29 @@
205205
カスタムマネジャとモデルの継承
206206
-------------------------------------
207207

208-
.. TBD
209-
210-
Class inheritance and model managers aren't quite a perfect match for each
211-
other. Managers are often specific to the classes they are defined on and
212-
inheriting them in subclasses isn't necessarily a good idea. Also, because the
213-
first manager declared is the *default manager*, it is important to allow that
214-
to be controlled. So here's how Django handles custom managers and
215-
:ref:`model inheritance <model-inheritance>`:
216-
217-
1. Managers defined on non-abstract base classes are *not* inherited by
218-
child classes. If you want to reuse a manager from a non-abstract base,
219-
redeclare it explicitly on the child class. These sorts of managers are
220-
likely to be fairly specific to the class they are defined on, so
221-
inheriting them can often lead to unexpected results (particularly as
222-
far as the default manager goes). Therefore, they aren't passed onto
223-
child classes.
224-
225-
2. Managers from abstract base classes are always inherited by the child
226-
class, using Python's normal name resolution order (names on the child
227-
class override all others; then come names on the first parent class,
228-
and so on). Abstract base classes are designed to capture information
229-
and behaviour that is common to their child classes. Defining common
230-
managers is an appropriate part of this common information.
231-
232-
3. The default manager on a class is either the first manager declared on
233-
the class, if that exists, or the default manager of the first abstract
234-
base class in the parent hierarchy, if that exists. If no default
235-
manager is explicitly declared, Django's normal default manager is
236-
used.
208+
クラスの継承とモデルのマネジャの関係は、お互い完全にしっくりきているわけで
209+
はありません。マネジャはたいていあるクラス固有のものなので、サブクラスでマ
210+
ネジャを継承するのは必ずしもよいアイデアとはいえないからです。また、最初に
211+
宣言されたマネジャが*デフォルトマネジャ* になることから、デフォルトマネジャ
212+
の制御が重要になってきます。そこで、ここでは、 Django がカスタムマネジャと
213+
:ref:`モデル継承 <model-inheritance>` をどのように扱うか説明します:
214+
215+
1. 抽象ベースクラスでないクラスで定義されたマネジャは、他のクラスに継承
216+
*されません* 。非抽象ベースクラスのマネジャを再利用したければ、
217+
子クラスで明示的に宣言してください。この種のマネジャは、たいていマネ
218+
ジャを定義しているクラス固有のもので、(デフォルトマネジャとして) 継
219+
承すると思わぬ結果を招くことがあるからです。そのため、子クラスに自動
220+
的に継承されないのです。
221+
222+
2. 抽象ベースクラスのマネジャは、通常の Python の名前解決規則
223+
(name resolution order, 子クラスの名前が他の名前をオーバライドする、
224+
親クラスリストの最初の親クラスの名前から順に参照する、など) に基づい
225+
て、常に子クラスに継承されます。抽象ベースクラスは、子クラスに共通し
226+
て持たせたい情報やふるまいを保持するためのクラスだからです。共通のマ
227+
ネジャの定義は、共通の情報として親クラスに置くのが適切です。
228+
229+
3. デフォルトマネジャは、そのクラスで最初に宣言されたマネジャクラスか、
230+
最初に見付かった抽象ベースクラスのデフォルトマネジャです。明示的なデ
231+
フォルトマネジャの設定がなければ、 Django の通常のデフォルトマネジャ
232+
を使います。
237233

0 commit comments

Comments
 (0)