django-dbgettext / docs / registration.rst

Registering Models

Models can be registered for django-dbgettext in a similar fashion to registering ModelAdmin classes for django.contrib.admin

Simply create a file within your application root directory, import the dbgettext registry object, and register your Models together with their customised dbgettext.models.Options. For example:

from dbgettext.registry import registry, Options
from dbgettext.lexicons import html
from myapp.models import MyModel

class MyModelOptions(Options):
    attributes = ('title',)
    parsed_attributes = {'body': html.lexicon}

registry.register(MyModel, MyModelOptions)

That's it. Your files will be automatically imported by django-dbgettext, and registered models will be included when running :doc:`dbgettext_export <dbgettext_export>`.

You can customise the module name using the DBGETTEXT_REGISTRATION_MODULE_NAME :ref:`setting <settings>`.

Project-level Registration

To register models from third-party applications, you can specify a module containing dbgettext registration in the DBGETTEXT_PROJECT_OPTIONS :ref:`setting <settings>`. For example, the following could be stored in my_project/ and DBGETTEXT_PROJECT_OPTIONS set to my_project.dbgettext_options to register flatpages

from dbgettext.registry import registry, Options
from dbgettext.lexicons import html
from django.contrib.flatpages.models import FlatPage

class FlatPageOptions(Options):
    attributes = ('title',)
    parsed_attributes = {'content': html.lexicon}

registry.register(FlatPage, FlatPageOptions)


  • attributes:

    tuple of names of fields/callables to be translated

  • parsed_attributes:

    dictionary of names of fields/callables with HTML content which should have translatable content extracted (should not be listed in attributes). Values are callables which take an Options argument and return a lexicon suitable for re.Scanner -- see dbgettext.lexicons.html for an example.

  • translate_if:

    dictionary used to filter() queryset

  • get_path_identifier:

    function returning string used to identify object in path to exported content (given an object)

  • parent:
    name of foreign key to parent model, if registered. Affects:
    • path (path_identifier appended onto parent path)
    • queryset (object only translated if parent is)
  • custom_lexicon_rules

    list of extra custom rules ((regexp, function) tuples) to be applied when parsing