1
1
.. _howto-initial-data:
2
2
3
3
=================================
4
- Providing initial data for models
4
+ モデルに初期データを与える
5
5
=================================
6
6
7
- .. TBD
8
-
9
7
:revision-up-to: 8961 (1.0)
10
8
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>`_ です。
18
14
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:
21
16
22
- Providing initial data with fixtures
17
+ フィクスチャによる初期データの投入
23
18
====================================
24
19
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
+ ください。
32
28
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
+ しておきます :
35
31
36
32
.. code-block:: js
37
33
@@ -53,8 +49,8 @@ look like in JSON:
53
49
}
54
50
},
55
51
]
56
-
57
- And here's that same fixture as YAML:
52
+
53
+ YAML で書くと以下のようになります:
58
54
59
55
.. code-block:: none
60
56
@@ -68,77 +64,80 @@ And here's that same fixture as YAML:
68
64
fields:
69
65
first_name: Paul
70
66
last_name: McCartney
71
-
72
- You'll store this data in a ``fixtures`` directory inside you app.
73
67
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`` ディレクトリに保存します。
80
69
81
- Automatically loading initial data fixtures
82
- -------------------------------------------
70
+ データのロードは簡単で、単に :djadmin:`manage.py loaddata fixturename
71
+ <loaddata>` を実行するだけです。 *fixturename* はフィクスチャファイルの名前
72
+ です。 :djadmin:`loaddata` を実行するたびに、フィクスチャデータが読み出され、
73
+ データベースにリロードされます。つまり、フィクスチャによって生成されたレコー
74
+ ド行を変更して :djadmin:`loaddata` を再度実行すると、変更後のデータは消去さ
75
+ れてしまうのです。注意してください。
83
76
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`` を使ってはなりません。
88
85
89
86
.. seealso::
90
87
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
+ 一貫したテスト環境を構築するためにも使われています。
93
90
94
91
.. _initial-sql:
95
92
96
- Providing initial SQL data
93
+ SQL による初期データの投入
97
94
==========================
98
95
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関数やビュー、トリガなどを作成したりできます。
103
100
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>`` は、モデル名を小文字にした文字列です。
106
104
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 文を指定できます。例えば以下のような命令を入れられます:
110
107
111
108
.. code-block:: sql
112
109
113
110
INSERT INTO myapp_person (first_name, last_name) VALUES ('John', 'Lennon');
114
111
INSERT INTO myapp_person (first_name, last_name) VALUES ('Paul', 'McCartney');
115
112
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