Skip to content

Commit 4cf17b0

Browse files
committed
small adds.
1 parent 6e4ee65 commit 4cf17b0

File tree

10 files changed

+629
-621
lines changed

10 files changed

+629
-621
lines changed

howto/custom-file-storage.txt

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
レージを作成したいときのために、カスタムストレージクラスを定義できます。
1212
カスタムストレージは、以下のステップに従って作成します:
1313

14-
.. TBD
15-
16-
#. Your custom storage system must be a subclass of
17-
``django.core.files.storage.Storage``::
14+
#. ストレージシステムは ``django.core.files.storage.Storage`` のサブクラス
15+
でなければなりません::
1816

1917
from django.core.files.storage import Storage
2018

2119
class MyStorage(Storage):
2220
...
2321

24-
#. Django must be able to instantiate your storage system without any arguments.
25-
This means that any settings should be taken from ``django.conf.settings``::
22+
#. Django はストレージシステムを引数なしでインスタンス化できねばなりません。
23+
すなわち、ストレージシステムに関する設定は、全て
24+
``django.conf.settings`` から取り出さねばなりません::
2625

2726
from django.conf import settings
2827
from django.core.files.storage import Storage
@@ -33,60 +32,63 @@
3332
option = settings.CUSTOM_STORAGE_OPTIONS
3433
...
3534

36-
#. Your storage class must implement the ``_open()`` and ``_save()`` methods,
37-
along with any other methods appropriate to your storage class. See below for
38-
more on these methods.
35+
#. ストレージクラスは、 ``_open()`` および ``_save()`` メソッドを実装せねば
36+
なりません。また、実現したいストレージクラスによってはその他にも適切なメ
37+
ソッドを実装せねばなりません。これらのメソッドについては下記を参照してく
38+
ださい。
3939

40-
In addition, if your class provides local file storage, it must override
41-
the ``path()`` method.
40+
さらに、ローカルのファイルを扱うストレージを提供する場合は、 ``path()``
41+
メソッドを提供せねばなりません。
4242

43-
Your custom storage system may override any of the storage methods explained in
44-
:ref:`ref-files-storage`, but you **must** implement the following methods:
43+
カスタムのストレージシステムでは、以下のストレージメソッドをオーバライドで
44+
きます。メソッドの詳細は :ref:`ref-files-storage` で解説していますが、少な
45+
くとも以下のメソッドは **実装せねばなりません** :
4546

4647
* :meth:`Storage.delete`
4748
* :meth:`Storage.exists`
4849
* :meth:`Storage.listdir`
4950
* :meth:`Storage.size`
5051
* :meth:`Storage.url`
5152

52-
You'all also usually want to use hooks specifically designed for custom storage
53-
objects. These are:
53+
また、カスタムのストレージオブジェクトを実装するために用意されている、以下
54+
のフックを使うことになるでしょう:
5455

5556
``_open(name, mode='rb')``
5657
~~~~~~~~~~~~~~~~~~~~~~~~~~
5758

58-
**Required**.
59+
**必須のメソッドです**.
5960

60-
Called by ``Storage.open()``, this is the actual mechanism the storage class
61-
uses to open the file. This must return a ``File`` object, though in most cases,
62-
you'll want to return some subclass here that implements logic specific to the
63-
backend storage system.
61+
``Storage.open()`` によって呼び出されます。このメソッドこそが、ストレージク
62+
ラスがファイルをオープンするときの実際のメカニズムです。このメソッドは
63+
``File`` クラスのオブジェクトを返さねばなりません。とはいえ、たいていのケー
64+
スでは、バックエンドストレージに固有のロジックを備えた ``File`` のサブクラ
65+
スを返すことでしょう。
6466

6567
``_save(name, content)``
6668
~~~~~~~~~~~~~~~~~~~~~~~~
6769

68-
Called by ``Storage.save()``. The ``name`` will already have gone through
69-
``get_valid_name()`` and ``get_available_name()``, and the ``content`` will be a
70-
``File`` object itself. No return value is expected.
70+
``Storage.save()`` から呼び出されます。 ``name`` は ``get_valid_name()``
71+
や ``get_available_name()`` で処理済の名前で、 ``content`` は ``File`` オブ
72+
ジェクトです。このメソッドの戻り値はありません。
7173

7274
``get_valid_name(name)``
7375
------------------------
7476

75-
Returns a filename suitable for use with the underlying storage system. The
76-
``name`` argument passed to this method is the original filename sent to the
77-
server, after having any path information removed. Override this to customize
78-
how non-standard characters are converted to safe filenames.
77+
背後にあるストレージシステムで扱うのに適したファイル名を生成して返します。
78+
通常、このメソッドの ``name`` 引数は、サーバに送信されたもとのファイルパス
79+
ら、パス部分を取り除いたファイルの名前です。標準の方法とは違うやりかたで安
80+
全なファイル名への変換を行いたいなら、このメソッドをオーバライドしてくださ
81+
い。
7982

80-
The code provided on ``Storage`` retains only alpha-numeric characters, periods
81-
and underscores from the original filename, removing everything else.
83+
``Storage`` の実装では、ファイル名から英数文字、ピリオド、アンダースコアを
84+
除く全ての文字を取り去ります。
8285

8386
``get_available_name(name)``
8487
----------------------------
8588

86-
Returns a filename that is available in the storage mechanism, possibly taking
87-
the provided filename into account. The ``name`` argument passed to this method
88-
will have already cleaned to a filename valid for the storage system, according
89-
to the ``get_valid_name()`` method described above.
89+
``name`` に指定された名前をもとに、ストレージ機構で扱えるファイル名を構築し
90+
て返します。通常、 ``name`` 引数は、上で述べた ``get_valid_name()`` メソッ
91+
ドによって、ストレージシステムで扱うのに適した名前に変換済みです。
9092

91-
The code provided on ``Storage`` simply appends underscores to the filename
92-
until it finds one that's available in the destination directory.
93+
``Storage`` の実装では、対象ディレクトリ下でファイル名が重複しそうな場合、
94+
一意になるまでアンダースコアを付加したファイル名を作成して返します。

howto/initial-data.txt

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
.. _howto-initial-data:
22

33
=================================
4-
Providing initial data for models
4+
モデルに初期データを与える
55
=================================
66

7-
.. TBD
8-
97
:revision-up-to: 8961 (1.0)
108

11-
It's sometimes useful to pre-populate your database with hard-coded data when
12-
you're first setting up an app. There's a couple of ways you can have Django
13-
automatically create this data: you can provide `initial data via fixtures`_, or
14-
you can provide `initial data as SQL`_.
15-
16-
In general, using a fixture is a cleaner method since it's database-agnostic,
17-
but initial SQL is also quite a bit more flexible.
9+
アプリケーションを最初にセットアップするときに、データベースにハードコード
10+
されたデータを投入できると便利なことがあります。 Django に自動的に初期デー
11+
タを投入させる方法は二つあります: ひとつは `フィクスチャによる初期データの
12+
投入 <providing-initial-data with-fixtures>`_ で、もう一つは `SQL による初
13+
期データの投入 <initial-sql>`_ です。
1814

19-
.. _initial data as sql: `providing initial sql data`_
20-
.. _initial data via fixtures: `providing initial data with fixtures`_
15+
.. _providing-initial-data with-fixtures:
2116

22-
Providing initial data with fixtures
17+
フィクスチャによる初期データの投入
2318
====================================
2419

25-
A fixture is a collection of data that Django knows how to import into a
26-
database. The most straightforward way of creating a fixture if you've already
27-
got some data is to use the :djadmin:`manage.py dumpdata` command. Or, you can
28-
write fixtures by hand; fixtures can be written as XML, YAML, or JSON documents.
29-
The :ref:`serialization documentation <topics-serialization>` has more details
30-
about each of these supported :ref:`serialization formats
31-
<serialization-formats>`.
20+
フィクスチャ (fixture) とは、 Django のデータベースに投入できるよう準備済み
21+
のデータの集まりです。すでにデータの入ったデータベースを持っているなら、最
22+
も簡単にフィクスチャを生成する方法は :djadmin:`manage.py dumpdata
23+
<dumpdata>` コマンドです。フィクスチャは手でも書けます。フィクスチャは XML,
24+
YAML または JSON ドキュメントとして書けるからです。サポートされている
25+
:ref:`シリアライゼーションフォーマット <serialization-formats>` の詳細は
26+
:ref:`シリアライゼーションのドキュメント <topics-serialization>` を参照して
27+
ください。
3228

33-
As an example, though, here's what a fixture for a simple ``Person`` model might
34-
look like in JSON:
29+
とはいえ、一例として、 JSON で書かれた ``Person`` モデルのフィクスチャを示
30+
しておきます:
3531

3632
.. code-block:: js
3733

@@ -53,8 +49,8 @@ look like in JSON:
5349
}
5450
},
5551
]
56-
57-
And here's that same fixture as YAML:
52+
53+
YAML で書くと以下のようになります:
5854

5955
.. code-block:: none
6056

@@ -68,77 +64,80 @@ And here's that same fixture as YAML:
6864
fields:
6965
first_name: Paul
7066
last_name: McCartney
71-
72-
You'll store this data in a ``fixtures`` directory inside you app.
7367

74-
Loading data is easy: just call :djadmin:`manage.py loaddata fixturename
75-
<loaddata>`, where *fixturename* is the name of the fixture file you've created.
76-
Every time you run :djadmin:`loaddata` the data will be read from the fixture
77-
and re-loaded into the database. Note that this means that if you change one of
78-
the rows created by a fixture and the run :djadmin:`loaddata` again you'll wipe
79-
out any changes you've made.
68+
フィクスチャデータはアプリケーションの ``fixture`` ディレクトリに保存します。
8069

81-
Automatically loading initial data fixtures
82-
-------------------------------------------
70+
データのロードは簡単で、単に :djadmin:`manage.py loaddata fixturename
71+
<loaddata>` を実行するだけです。 *fixturename* はフィクスチャファイルの名前
72+
です。 :djadmin:`loaddata` を実行するたびに、フィクスチャデータが読み出され、
73+
データベースにリロードされます。つまり、フィクスチャによって生成されたレコー
74+
ド行を変更して :djadmin:`loaddata` を再度実行すると、変更後のデータは消去さ
75+
れてしまうのです。注意してください。
8376

84-
If you create a fixture named ``initial_data.[xml/yml/json]``, that fixture will
85-
be loaded every time you run :djadmin:`syncdb`. This is extremely convenient,
86-
but be careful: remember that the data will be refreshed *every time* you run
87-
:djadmin:`syncdb`. So don't use ``initial_data`` for data you'll want to edit.
77+
初期データフィクスチャの自動ロード
78+
-----------------------------------
79+
80+
``initial_data.[xml/yml/json]`` という名前のフィクスチャを作成しておくと、
81+
:djadmin:`syncdb` を実行するたびに自動的にロードされます。この機能はとても
82+
便利なのですが、一つだけ注意が必要です。というのも、 :djadmin:`syncdb`
83+
を実行するたびに *毎回* データがリフレッシュされるからです。ですから、
84+
変更する予定のデータに ``initial_data`` を使ってはなりません。
8885

8986
.. seealso::
9087

91-
Fixtures are also used by the :ref:`testing framework
92-
<topics-testing-fixtures>` to help set up a consistent test environment.
88+
フィクスチャは :ref:`テストフレームワーク <topics-testing-fixtures>` で
89+
一貫したテスト環境を構築するためにも使われています。
9390

9491
.. _initial-sql:
9592

96-
Providing initial SQL data
93+
SQL による初期データの投入
9794
==========================
9895

99-
Django provides a hook for passing the database arbitrary SQL that's executed
100-
just after the CREATE TABLE statements when you run :djadmin:`syncdb`. You can
101-
use this hook to populate default records, or you could also create SQL
102-
functions, views, triggers, etc.
96+
Django には、データベースに任意の SQL を渡すためのフックがあります。 この
97+
SQL は、 :djadmin:`syncdb` を実行した時に CREATE TABLE 文の直後に実行されま
98+
す。このフックを使えば、自動的にデフォルトのレコードをテーブルに追加したり、
99+
SQL関数やビュー、トリガなどを作成したりできます。
103100

104-
The hook is simple: Django just looks for a file called ``sql/<modelname>.sql``,
105-
in your app directory, where ``<modelname>`` is the model's name in lowercase.
101+
フックのからくりは単純です: Django はアプリケーション内の
102+
``sql/<modelname>.sql`` という名前のファイルを探し、実行するだけです。
103+
``<modelname>`` は、モデル名を小文字にした文字列です。
106104

107-
So, if you had a ``Person`` model in an app called ``myapp``, you could add
108-
arbitrary SQL to the file ``sql/person.sql`` inside your ``myapp`` directory.
109-
Here's an example of what the file might contain:
105+
このドキュメントの冒頭にある ``Person`` の例で、モデルが ``myapp`` の下に置
106+
かれていたとすると、 ``myapp/sql/person.sql`` というファイルに任意の SQL 文を指定できます。例えば以下のような命令を入れられます:
110107

111108
.. code-block:: sql
112109

113110
INSERT INTO myapp_person (first_name, last_name) VALUES ('John', 'Lennon');
114111
INSERT INTO myapp_person (first_name, last_name) VALUES ('Paul', 'McCartney');
115112

116-
Each SQL file, if given, is expected to contain valid SQL statements
117-
which will insert the desired data (e.g., properly-formatted
118-
``INSERT`` statements separated by semicolons).
119-
120-
The SQL files are read by the :djadmin:`sqlcustom`, :djadmin:`sqlreset`,
121-
:djadmin:`sqlall` and :djadmin:`reset` commands in :ref:`manage.py
122-
<ref-django-admin>`. Refer to the :ref:`manage.py documentation
123-
<ref-django-admin>` for more information.
124-
125-
Note that if you have multiple SQL data files, there's no guarantee of the order
126-
in which they're executed. The only thing you can assume is that, by the time
127-
your custom data files are executed, all the database tables already will have
128-
been created.
129-
130-
Database-backend-specific SQL data
131-
----------------------------------
132-
133-
There's also a hook for backend-specific SQL data. For example, you can have
134-
separate initial-data files for PostgreSQL and MySQL. For each app, Django
135-
looks for a file called ``<appname>/sql/<modelname>.<backend>.sql``, where
136-
``<appname>`` is your app directory, ``<modelname>`` is the model's name in
137-
lowercase and ``<backend>`` is the value of :setting:`DATABASE_ENGINE` in your
138-
settings file (e.g., ``postgresql``, ``mysql``).
139-
140-
Backend-specific SQL data is executed before non-backend-specific SQL data. For
141-
example, if your app contains the files ``sql/person.sql`` and
142-
``sql/person.postgresql.sql`` and you're installing the app on PostgreSQL,
143-
Django will execute the contents of ``sql/person.postgresql.sql`` first, then
144-
``sql/person.sql``.
113+
各 SQL ファイルには、必要なデータを ``INSERT`` するための有効な SQL 文を
114+
(すなわち、正しくフォーマットされた ``INSERT`` 文をセミコロンで区切って) 入
115+
れておかねばなりません。
116+
117+
SQL ファイルは :ref:`manage.py <ref-django-admin>` の :djadmin:`sqlcustom`
118+
:djadmin:`sqlreset`, :djadmin:`sqlall` および :djadmin:`reset` コマンドの実
119+
行時に参照されます。詳しくは :ref:`manage.py のドキュメント
120+
<ref-django-admin>` を参照してください。
121+
122+
複数の SQL データファイルがある場合、個々のファイルを実行する順番は保証され
123+
ていないので注意して下さい。仮定していてよいのは、カスタムの SQL データファ
124+
イルを実行する前に、必ずデータベーステーブルは作成されているということだけ
125+
です。
126+
127+
データベースバックエンド特有の SQL データ
128+
-----------------------------------------
129+
130+
バックエンド特有の SQL データに対するフックもあります。例えば、 PostgreSQL
131+
と MySQL 向けに別々の初期データを用意できます。各アプリケーションごとに
132+
Django は ``<appname>/sql/<modelname>.<backend>.sql`` というファイルを探し
133+
ます。 ``<appname>`` はアプリケーションディレクトリの名前、 ``<modelname>``
134+
はモデル名を小文字にした文字列、 ``<backend>`` は設定ファイルの
135+
:setting:`DATABASE_ENGINE` に指定するバックエンドの名前 (``postgresql``,
136+
``mysql`` など) です。
137+
138+
バックエンド固有の SQL データは、バックエンド非固有の SQL データよりも前に
139+
実行されます。例えば、アプリケーション中に ``sql/person.sql`` および
140+
``sql/person.postgresql.sql`` が入っていて、 PostgreSQL をバックエンドにし
141+
てインストールを行った場合、 Django はまず ``sql/person.postgresql.sql`` の
142+
内容を実行してから ``sql/person.sql`` を実行します。
143+

0 commit comments

Comments
 (0)