/usr/share/doc/python-django-modeltranslation/html/registration.html is in python-django-modeltranslation-doc 0.12.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registering Models for Translation — django-modeltranslation 0.12.2 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.12.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Accessing Translated and Translation Fields" href="usage.html" />
<link rel="prev" title="Installation" href="installation.html" />
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="usage.html" title="Accessing Translated and Translation Fields"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installation"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">django-modeltranslation 0.12.2 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="registering-models-for-translation">
<span id="registration"></span><h1>Registering Models for Translation<a class="headerlink" href="#registering-models-for-translation" title="Permalink to this headline">¶</a></h1>
<p>Modeltranslation can translate model fields of any model class. For each model
to translate a translation option class containing the fields to translate is
registered with a special object called the <code class="docutils literal"><span class="pre">translator</span></code>.</p>
<p>Registering models and their fields for translation requires the following
steps:</p>
<ol class="arabic simple">
<li>Create a <code class="docutils literal"><span class="pre">translation.py</span></code> in your app directory.</li>
<li>Create a translation option class for every model to translate.</li>
<li>Register the model and the translation option class at
<code class="docutils literal"><span class="pre">modeltranslation.translator.translator</span></code>.</li>
</ol>
<p>The modeltranslation application reads the <code class="docutils literal"><span class="pre">translation.py</span></code> file in your
app directory thereby triggering the registration of the translation
options found in the file.</p>
<p>A translation option is a class that declares which fields of a model to
translate. The class must derive from
<code class="docutils literal"><span class="pre">modeltranslation.translator.TranslationOptions</span></code> and it must provide a
<code class="docutils literal"><span class="pre">fields</span></code> attribute storing the list of fieldnames. The option class must be
registered with the <code class="docutils literal"><span class="pre">modeltranslation.translator.translator</span></code> instance.</p>
<p>To illustrate this let’s have a look at a simple example using a <code class="docutils literal"><span class="pre">News</span></code>
model. The news in this example only contains a <code class="docutils literal"><span class="pre">title</span></code> and a <code class="docutils literal"><span class="pre">text</span></code> field.
Instead of a news, this could be any Django model class:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">News</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
<span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">)</span>
<span class="n">text</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
</pre></div>
</div>
<p>In order to tell modeltranslation to translate the <code class="docutils literal"><span class="pre">title</span></code> and <code class="docutils literal"><span class="pre">text</span></code> fields,
create a <code class="docutils literal"><span class="pre">translation.py</span></code> file in your news app directory and add the
following:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">modeltranslation.translator</span> <span class="k">import</span> <span class="n">translator</span><span class="p">,</span> <span class="n">TranslationOptions</span>
<span class="kn">from</span> <span class="nn">news.models</span> <span class="k">import</span> <span class="n">News</span>
<span class="k">class</span> <span class="nc">NewsTranslationOptions</span><span class="p">(</span><span class="n">TranslationOptions</span><span class="p">):</span>
<span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">,)</span>
<span class="n">translator</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">News</span><span class="p">,</span> <span class="n">NewsTranslationOptions</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that this does not require to change the <code class="docutils literal"><span class="pre">News</span></code> model in any way, it’s
only imported. The <code class="docutils literal"><span class="pre">NewsTranslationOptions</span></code> derives from
<code class="docutils literal"><span class="pre">TranslationOptions</span></code> and provides the <code class="docutils literal"><span class="pre">fields</span></code> attribute. Finally the model
and its translation options are registered at the <code class="docutils literal"><span class="pre">translator</span></code> object.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.10.</span></p>
</div>
<p>If you prefer, <code class="docutils literal"><span class="pre">register</span></code> is also available as a decorator, much like the
one Django introduced for its admin in version 1.7. Usage is similar to the
standard <code class="docutils literal"><span class="pre">register</span></code>, just provide arguments as you normally would, except
the options class which will be the decorated one:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">modeltranslation.translator</span> <span class="k">import</span> <span class="n">register</span><span class="p">,</span> <span class="n">TranslationOptions</span>
<span class="kn">from</span> <span class="nn">news.models</span> <span class="k">import</span> <span class="n">News</span>
<span class="nd">@register</span><span class="p">(</span><span class="n">News</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">NewsTranslationOptions</span><span class="p">(</span><span class="n">TranslationOptions</span><span class="p">):</span>
<span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">,)</span>
</pre></div>
</div>
<p>At this point you are mostly done and the model classes registered for
translation will have been added some auto-magical fields. The next section
explains how things are working under the hood.</p>
<div class="section" id="translationoptions-fields-inheritance">
<span id="to-field-inheritance"></span><h2><code class="docutils literal"><span class="pre">TranslationOptions</span></code> fields inheritance<a class="headerlink" href="#translationoptions-fields-inheritance" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.</span></p>
</div>
<p>A subclass of any <code class="docutils literal"><span class="pre">TranslationOptions</span></code> will inherit fields from its bases
(similar to the way Django models inherit fields, but with a different syntax).</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">modeltranslation.translator</span> <span class="k">import</span> <span class="n">translator</span><span class="p">,</span> <span class="n">TranslationOptions</span>
<span class="kn">from</span> <span class="nn">news.models</span> <span class="k">import</span> <span class="n">News</span><span class="p">,</span> <span class="n">NewsWithImage</span>
<span class="k">class</span> <span class="nc">NewsTranslationOptions</span><span class="p">(</span><span class="n">TranslationOptions</span><span class="p">):</span>
<span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">,)</span>
<span class="k">class</span> <span class="nc">NewsWithImageTranslationOptions</span><span class="p">(</span><span class="n">NewsTranslationOptions</span><span class="p">):</span>
<span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'image'</span><span class="p">,)</span>
<span class="n">translator</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">News</span><span class="p">,</span> <span class="n">NewsTranslationOptions</span><span class="p">)</span>
<span class="n">translator</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">NewsWithImage</span><span class="p">,</span> <span class="n">NewsWithImageTranslationOptions</span><span class="p">)</span>
</pre></div>
</div>
<p>The above example adds the fields <code class="docutils literal"><span class="pre">title</span></code> and <code class="docutils literal"><span class="pre">text</span></code> from the
<code class="docutils literal"><span class="pre">NewsTranslationOptions</span></code> class to <code class="docutils literal"><span class="pre">NewsWithImageTranslationOptions</span></code>, or to
say it in code:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">assert</span> <span class="n">NewsWithImageTranslationOptions</span><span class="o">.</span><span class="n">fields</span> <span class="o">==</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">,</span> <span class="s1">'image'</span><span class="p">)</span>
</pre></div>
</div>
<p>Of course multiple inheritance and inheritance chains (A > B > C) also work as
expected.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When upgrading from a previous modeltranslation version (<0.5), please
review your <code class="docutils literal"><span class="pre">TranslationOptions</span></code> classes and see if introducing <cite>fields
inheritance</cite> broke the project (if you had always subclassed
<code class="docutils literal"><span class="pre">TranslationOptions</span></code> only, there is no risk).</p>
</div>
</div>
<div class="section" id="changes-automatically-applied-to-the-model-class">
<h2>Changes Automatically Applied to the Model Class<a class="headerlink" href="#changes-automatically-applied-to-the-model-class" title="Permalink to this headline">¶</a></h2>
<p>After registering the <code class="docutils literal"><span class="pre">News</span></code> model for translation a SQL dump of the news
app will look like this:</p>
<div class="highlight-console"><div class="highlight"><pre><span></span><span class="gp">$</span> ./manage.py sqlall news
<span class="go">BEGIN;</span>
<span class="go">CREATE TABLE `news_news` (</span>
<span class="go"> `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,</span>
<span class="go"> `title` varchar(255) NOT NULL,</span>
<span class="go"> `title_de` varchar(255) NULL,</span>
<span class="go"> `title_en` varchar(255) NULL,</span>
<span class="go"> `text` longtext NULL,</span>
<span class="go"> `text_de` longtext NULL,</span>
<span class="go"> `text_en` longtext NULL,</span>
<span class="go">)</span>
<span class="go">;</span>
<span class="go">CREATE INDEX `news_news_page_id` ON `news_news` (`page_id`);</span>
<span class="go">COMMIT;</span>
</pre></div>
</div>
<p>Note the <code class="docutils literal"><span class="pre">title_de</span></code>, <code class="docutils literal"><span class="pre">title_en</span></code>, <code class="docutils literal"><span class="pre">text_de</span></code> and <code class="docutils literal"><span class="pre">text_en</span></code> fields which
are not declared in the original <code class="docutils literal"><span class="pre">News</span></code> model class but rather have been
added by the modeltranslation app. These are called <em>translation fields</em>. There
will be one for every language in your project’s <code class="docutils literal"><span class="pre">settings.py</span></code>.</p>
<p>The name of these additional fields is build using the original name of the
translated field and appending one of the language identifiers found in the
<code class="docutils literal"><span class="pre">settings.LANGUAGES</span></code>.</p>
<p>As these fields are added to the registered model class as fully valid Django
model fields, they will appear in the db schema for the model although it has
not been specified on the model explicitly.</p>
<div class="section" id="precautions-regarding-registration-approach">
<span id="register-precautions"></span><h3>Precautions regarding registration approach<a class="headerlink" href="#precautions-regarding-registration-approach" title="Permalink to this headline">¶</a></h3>
<p>Be aware that registration approach (as opposed to base-class approach) to
models translation has a few caveats, though (despite many pros).</p>
<p>First important thing to note is the fact that translatable models are being patched - that means
their fields list is not final until the modeltranslation code executes. In normal circumstances
it shouldn’t affect anything - as long as <code class="docutils literal"><span class="pre">models.py</span></code> contain only models’ related code.</p>
<p>For example: consider a project where a <code class="docutils literal"><span class="pre">ModelForm</span></code> is declared in <code class="docutils literal"><span class="pre">models.py</span></code> just after
its model. When the file is executed, the form gets prepared - but it will be frozen with
old fields list (without translation fields). That’s because the <code class="docutils literal"><span class="pre">ModelForm</span></code> will be created
before modeltranslation would add new fields to the model (<code class="docutils literal"><span class="pre">ModelForm</span></code> gather fields info at class
creation time, not instantiation time). Proper solution is to define the form in <code class="docutils literal"><span class="pre">forms.py</span></code>,
which wouldn’t be imported alongside with <code class="docutils literal"><span class="pre">models.py</span></code> (and rather imported from views file or
urlconf).</p>
<p>Generally, for seamless integration with modeltranslation (and as sensible design anyway),
the <code class="docutils literal"><span class="pre">models.py</span></code> should contain only bare models and model related logic.</p>
</div>
<div class="section" id="committing-fields-to-database">
<span id="db-fields"></span><h3>Committing fields to database<a class="headerlink" href="#committing-fields-to-database" title="Permalink to this headline">¶</a></h3>
<p>If you are starting a fresh project and have considered your translation needs
in the beginning then simply sync your database (<code class="docutils literal"><span class="pre">./manage.py</span> <span class="pre">syncdb</span></code> or
<code class="docutils literal"><span class="pre">./manage.py</span> <span class="pre">schemamigration</span> <span class="pre">myapp</span> <span class="pre">--initial</span></code> if using South)
and you are ready to use the translated models.</p>
<p>In case you are translating an existing project and your models have already
been synced to the database you will need to alter the tables in your database
and add these additional translation fields. If you are using South, you’re
done: simply create a new migration (South will detect newly added translation
fields) and apply it. If not, you can use a little helper:
<a class="reference internal" href="commands.html#commands-sync-translation-fields"><span class="std std-ref">The sync_translation_fields Command</span></a> which can execute schema-ALTERing SQL
to add new fields. Use either of these two solutions, not both.</p>
<p>If you are adding translation fields to a third-party app that is using South,
things get more complicated. In order to be able to update the app in the future,
and to feel comfortable, you should use the <code class="docutils literal"><span class="pre">sync_translation_fields</span></code> command.
Although it’s possible to introduce new fields in a migration, it’s nasty and
involves copying migration files, using <code class="docutils literal"><span class="pre">SOUTH_MIGRATION_MODULES</span></code> setting,
and passing <code class="docutils literal"><span class="pre">--delete-ghost-migrations</span></code> flag, so we don’t recommend it.
Invoking <code class="docutils literal"><span class="pre">sync_translation_fields</span></code> is plain easier.</p>
<p>Note that all added fields are by default declared <code class="docutils literal"><span class="pre">blank=True</span></code> and
<code class="docutils literal"><span class="pre">null=True</span></code> no matter if the original field is required or not. In other
words - all translations are optional, unless an explicit option is
provided - see <a class="reference internal" href="#required-langs"><span class="std std-ref">Required fields</span></a>.</p>
<p>To populate the default translation fields added by modeltranslation with
values from existing database fields, you can use the
<code class="docutils literal"><span class="pre">update_translation_fields</span></code> command. See
<a class="reference internal" href="commands.html#commands-update-translation-fields"><span class="std std-ref">The update_translation_fields Command</span></a> for more info on this.</p>
<div class="section" id="migrations-django-1-7">
<span id="migrations"></span><h4>Migrations (Django 1.7)<a class="headerlink" href="#migrations-django-1-7" title="Permalink to this headline">¶</a></h4>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.8.</span></p>
</div>
<p>Modeltranslation supports the migration system introduced by Django 1.7.
Besides the normal workflow as described in Django’s <a class="reference external" href="https://docs.djangoproject.com/en/dev/topics/migrations/#workflow">Migration docs</a>, you
should do a migration whenever one of the following changes have been made to
your project:</p>
<ul class="simple">
<li>Added or removed a language through <code class="docutils literal"><span class="pre">settings.LANGUAGES</span></code> or
<code class="docutils literal"><span class="pre">settings.MODELTRANSLATION</span> <span class="pre">LANGUAGES</span></code>.</li>
<li>Registered or unregistered a field through <code class="docutils literal"><span class="pre">TranslationOptions.fields</span></code>.</li>
</ul>
<p>It doesn’t matter if you are starting a fresh project or change an existing
one, it’s always:</p>
<ol class="arabic simple">
<li><code class="docutils literal"><span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">makemigrations</span></code> to create a new migration with
the added or removed fields.</li>
<li><code class="docutils literal"><span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">migrate</span></code> to apply the changes.</li>
</ol>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Support for migrations is implemented through
<code class="docutils literal"><span class="pre">fields.TranslationField.deconstruct(self)</span></code> and respects changes to the
<code class="docutils literal"><span class="pre">null</span></code> option.</p>
</div>
</div>
</div>
</div>
<div class="section" id="required-fields">
<span id="required-langs"></span><h2>Required fields<a class="headerlink" href="#required-fields" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.8.</span></p>
</div>
<p>By default, all translation fields are optional (not required). This can be
changed using a special attribute on <code class="docutils literal"><span class="pre">TranslationOptions</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">NewsTranslationOptions</span><span class="p">(</span><span class="n">TranslationOptions</span><span class="p">):</span>
<span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">,)</span>
<span class="n">required_languages</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'en'</span><span class="p">,</span> <span class="s1">'de'</span><span class="p">)</span>
</pre></div>
</div>
<p>It’s quite self-explanatory: for German and English, all translation fields are required. For other
languages - optional.</p>
<p>A more fine-grained control is available:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">NewsTranslationOptions</span><span class="p">(</span><span class="n">TranslationOptions</span><span class="p">):</span>
<span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">,)</span>
<span class="n">required_languages</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'de'</span><span class="p">:</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">),</span> <span class="s1">'default'</span><span class="p">:</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,)}</span>
</pre></div>
</div>
<p>For German, all fields (both <code class="docutils literal"><span class="pre">title</span></code> and <code class="docutils literal"><span class="pre">text</span></code>) are required; for all other languages - only
<code class="docutils literal"><span class="pre">title</span></code> is required. The <code class="docutils literal"><span class="pre">'default'</span></code> is optional.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Requirement is enforced by <code class="docutils literal"><span class="pre">blank=False</span></code>. Please remember that it will trigger validation only
in modelforms and admin (as always in Django). Manual model validation can be performed via
the <code class="docutils literal"><span class="pre">full_clean()</span></code> model method.</p>
<p class="last">The required fields are still <code class="docutils literal"><span class="pre">null=True</span></code>, though.</p>
</div>
</div>
<div class="section" id="translationoptions-attributes-reference">
<h2><code class="docutils literal"><span class="pre">TranslationOptions</span></code> attributes reference<a class="headerlink" href="#translationoptions-attributes-reference" title="Permalink to this headline">¶</a></h2>
<p>Quick cheatsheet with links to proper docs sections and examples showing expected syntax.</p>
<p>Classes inheriting from <code class="docutils literal"><span class="pre">TranslationOptions</span></code> can have following attributes defined:</p>
<dl class="attribute">
<dt id="TranslationOptions.fields">
<code class="descclassname">TranslationOptions.</code><code class="descname">fields</code><span class="sig-paren">(</span><em>required</em><span class="sig-paren">)</span><a class="headerlink" href="#TranslationOptions.fields" title="Permalink to this definition">¶</a></dt>
<dd><p>List of translatable model fields. See <a class="reference internal" href="#registration"><span class="std std-ref">Registering Models for Translation</span></a>.</p>
<p>Some fields can be implicitly added through inheritance, see <a class="reference internal" href="#to-field-inheritance"><span class="std std-ref">TranslationOptions fields inheritance</span></a>.</p>
</dd></dl>
<dl class="attribute">
<dt id="TranslationOptions.fallback_languages">
<code class="descclassname">TranslationOptions.</code><code class="descname">fallback_languages</code><a class="headerlink" href="#TranslationOptions.fallback_languages" title="Permalink to this definition">¶</a></dt>
<dd><p>Control order of languages for fallback purposes. See <a class="reference internal" href="usage.html#fallback-lang"><span class="std std-ref">Fallback languages</span></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">fallback_languages</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'default'</span><span class="p">:</span> <span class="p">(</span><span class="s1">'en'</span><span class="p">,</span> <span class="s1">'de'</span><span class="p">,</span> <span class="s1">'fr'</span><span class="p">),</span> <span class="s1">'uk'</span><span class="p">:</span> <span class="p">(</span><span class="s1">'ru'</span><span class="p">,)}</span>
</pre></div>
</div>
</dd></dl>
<dl class="attribute">
<dt id="TranslationOptions.fallback_values">
<code class="descclassname">TranslationOptions.</code><code class="descname">fallback_values</code><a class="headerlink" href="#TranslationOptions.fallback_values" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the value that should be used if no fallback language yielded a value.
See <a class="reference internal" href="usage.html#fallback-val"><span class="std std-ref">Fallback values</span></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">fallback_values</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">'-- sorry, no translation provided --'</span><span class="p">)</span>
<span class="n">fallback_values</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'title'</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">'Object not translated'</span><span class="p">),</span> <span class="s1">'text'</span><span class="p">:</span> <span class="s1">'---'</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>
<dl class="attribute">
<dt id="TranslationOptions.fallback_undefined">
<code class="descclassname">TranslationOptions.</code><code class="descname">fallback_undefined</code><a class="headerlink" href="#TranslationOptions.fallback_undefined" title="Permalink to this definition">¶</a></dt>
<dd><p>Set what value should be considered “no value”. See <a class="reference internal" href="usage.html#fallback-undef"><span class="std std-ref">Fallback undefined</span></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">fallback_undefined</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">fallback_undefined</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'title'</span><span class="p">:</span> <span class="s1">'no title'</span><span class="p">,</span> <span class="s1">'text'</span><span class="p">:</span> <span class="kc">None</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>
<dl class="attribute">
<dt id="TranslationOptions.empty_values">
<code class="descclassname">TranslationOptions.</code><code class="descname">empty_values</code><a class="headerlink" href="#TranslationOptions.empty_values" title="Permalink to this definition">¶</a></dt>
<dd><p>Override the value that should be saved in forms on empty fields.
See <a class="reference internal" href="forms.html#formfield-nullability"><span class="std std-ref">Formfields and nullability</span></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">empty_values</span> <span class="o">=</span> <span class="s1">''</span>
<span class="n">empty_values</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'title'</span><span class="p">:</span> <span class="s1">''</span><span class="p">,</span> <span class="s1">'slug'</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">'desc'</span><span class="p">:</span> <span class="s1">'both'</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>
<dl class="attribute">
<dt id="TranslationOptions.required_languages">
<code class="descclassname">TranslationOptions.</code><code class="descname">required_languages</code><a class="headerlink" href="#TranslationOptions.required_languages" title="Permalink to this definition">¶</a></dt>
<dd><p>Control which translation fields are required. See <a class="reference internal" href="#required-langs"><span class="std std-ref">Required fields</span></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">required_languages</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'en'</span><span class="p">,</span> <span class="s1">'de'</span><span class="p">)</span>
<span class="n">required_languages</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'de'</span><span class="p">:</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,</span><span class="s1">'text'</span><span class="p">),</span> <span class="s1">'default'</span><span class="p">:</span> <span class="p">(</span><span class="s1">'title'</span><span class="p">,)}</span>
</pre></div>
</div>
</dd></dl>
</div>
<div class="section" id="supported-fields-matrix">
<span id="supported-field-matrix"></span><h2>Supported Fields Matrix<a class="headerlink" href="#supported-fields-matrix" title="Permalink to this headline">¶</a></h2>
<p>While the main purpose of modeltranslation is to translate text-like fields,
translating other fields can be useful in several situations. The table lists
all model fields available in Django and gives an overview about their current
support status:</p>
<table border="1" class="docutils">
<colgroup>
<col width="78%" />
<col width="8%" />
<col width="8%" />
<col width="8%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Model Field</th>
<th class="head">0.4</th>
<th class="head">0.5</th>
<th class="head">0.7</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">AutoField</span></code></td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">BigIntegerField</span></code></td>
<td>No</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">BooleanField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">CharField</span></code></td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">CommaSeparatedIntegerField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">DateField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">DateTimeField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">DecimalField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">EmailField</span></code></td>
<td>Yes*</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">FileField</span></code></td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">FilePathField</span></code></td>
<td>Yes*</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">FloatField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">ImageField</span></code></td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">IntegerField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">IPAddressField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">GenericIPAddressField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">NullBooleanField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">PositiveIntegerField</span></code></td>
<td>No</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">PositiveSmallIntegerField</span></code></td>
<td>No</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">SlugField</span></code></td>
<td>Yes*</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">SmallIntegerField</span></code></td>
<td>No</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">TextField</span></code></td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">TimeField</span></code></td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">URLField</span></code></td>
<td>Yes*</td>
<td>Yes*</td>
<td>Yes*</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">ForeignKey</span></code></td>
<td>No</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">OneToOneField</span></code></td>
<td>No</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">ManyToManyField</span></code></td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p>* Implicitly supported (as subclass of a supported field)</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Registering Models for Translation</a><ul>
<li><a class="reference internal" href="#translationoptions-fields-inheritance"><code class="docutils literal"><span class="pre">TranslationOptions</span></code> fields inheritance</a></li>
<li><a class="reference internal" href="#changes-automatically-applied-to-the-model-class">Changes Automatically Applied to the Model Class</a><ul>
<li><a class="reference internal" href="#precautions-regarding-registration-approach">Precautions regarding registration approach</a></li>
<li><a class="reference internal" href="#committing-fields-to-database">Committing fields to database</a><ul>
<li><a class="reference internal" href="#migrations-django-1-7">Migrations (Django 1.7)</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#required-fields">Required fields</a></li>
<li><a class="reference internal" href="#translationoptions-attributes-reference"><code class="docutils literal"><span class="pre">TranslationOptions</span></code> attributes reference</a></li>
<li><a class="reference internal" href="#supported-fields-matrix">Supported Fields Matrix</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="installation.html"
title="previous chapter">Installation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="usage.html"
title="next chapter">Accessing Translated and Translation Fields</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/registration.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="usage.html" title="Accessing Translated and Translation Fields"
>next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installation"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">django-modeltranslation 0.12.2 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2009-2018, Peter Eschler, Dirk Eschler, Jacek Tomaszewski.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
</div>
</body>
</html>
|