Skip to main content

A content translation framework for django integrated automatically in the normal ORM. Removes the pain of having to think about translations in a django project.

Project description

Model translations made easy.

This project adds support for model translations in Django. It is designed to be unobtrusive, efficient and reliable. On the technical side, it uses an automatically created Translations Model to store translatable fields in arbitrary languages with a foreign key to the main model, enabling fast queries.

Started in 2011, hvad has grown mature and is now used on large scale applications.

Quick links:

Features

  • Simple - only 3 new queryset methods.

  • Natural - use Django ORM as usual, it just became language aware.

  • Fast - no additional queries for reads, just an inner join to an indexed key.

  • Complete - relationships, custom managers and querysets, proxy models, and abstract models.

  • Batteries included - translation-enabled forms and admin are provided.

  • Reliable - more than 300 test cases and counting. coverage build

  • Compatible with Django 1.8 to 1.11, running Python 2.7, 3.4, 3.5 or 3.6.

Django-hvad also features support for Django REST framework 3.1 or newer, including translation-aware serializers.

Example Uses

Declaring a translatable Book model:

class Book(TranslatableModel):
    author = models.ForeignKey(Author)
    release = models.Date()

    translations = TranslatedFields(
        title = models.CharField(max_length=250)
    )

Thus, only the title will vary based on the language. Release date and author are shared among all languages. Let’s now create a Book instance:

# The recommended way:
book = Book.objects.language('en').create(
    author = Author.objects.get(name='Antoine de Saint Exupéry'),
    release = datetime.date(1943, 4, 6),
    title = "The Little Prince",
)

# Also works
book = Book(language_code='en')
book.author = Author.objects.get(name='Antoine de Saint Exupéry')
book.release = datetime.date(1943, 4, 6)
book.title = "The Little Prince"
book.save()

Providing some translations:

book.translate('fr')
book.title = "Le Petit Prince"
book.save()
book.translate('de')
book.title = "Der kleine Prinz"
book.save()

Every call to translate() creates a new translation from scratch and switches to that translation; save() only saves the latest translation. Let’s now perform some language-aware queries:

Book.objects.all()

Compatible by default: returns all objects, without any translated fields attached. Starting from v1.0, default behavior can be overriden to work like next query:

Book.objects.language().all()

Returns all objects as translated instances, but only the ones that are translated into the currect language. You can also specify which language to get, using e.g.:

Book.objects.language("en").all()

Usual queryset methods work like they always did: let’s get all books as translated instances, filtering on the title attribute, returning those that have Petit Prince in their French title, ordered by publication date (in their French edition):

Book.objects.language("fr").filter(title__contains='Petit Prince').order_by('release')

Other random examples:

# last German book published in year 1948
Book.objects.language("de").filter(release__year=1948).latest()

# other books from the same author as mybook. Cache author as well.
Book.objects.language().select_related('author').filter(author__books=mybook)

# books that have "Django" in their title, regardless of the language
Book.objects.language('all').filter(title__icontains='Django')

More examples in the quickstart guide.

Releases

Django-hvad uses the same release pattern as Django. The following versions are thus available:

  • Stable branch 1.7, available through PyPI and git branch releases/1.7.x.

  • Stable branch 1.8, available through PyPI and git branch releases/1.8.x.

  • Development branch 1.9, available through git branch master.

Stable branches have minor bugfix releases as needed, with guaranteed compatibility. See the installation guide for details, or have a look at the release notes.

Thanks to

Jonas Obrist (https://github.com/ojii) for making django-nani and for helping me with this project.

Kristian Øllegaard (https://github.com/KristianOellegaard/) for django-hvad and trusting me to continue the development.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lotrek-django-hvad-1.8.1.tar.gz (100.8 kB view details)

Uploaded Source

Built Distribution

lotrek_django_hvad-1.8.1-py2.py3-none-any.whl (105.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file lotrek-django-hvad-1.8.1.tar.gz.

File metadata

  • Download URL: lotrek-django-hvad-1.8.1.tar.gz
  • Upload date:
  • Size: 100.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.9

File hashes

Hashes for lotrek-django-hvad-1.8.1.tar.gz
Algorithm Hash digest
SHA256 1e07394dd7359d504bac1ce75d9a8ac14a0124bc13b2efa028682f331ff5caa0
MD5 12a54d859b3426b1d060466d8e7f10bc
BLAKE2b-256 9ca94174e64fd45655c4370b8bc3a40b9bff4c42efa040f7d66b6705147efd91

See more details on using hashes here.

File details

Details for the file lotrek_django_hvad-1.8.1-py2.py3-none-any.whl.

File metadata

  • Download URL: lotrek_django_hvad-1.8.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 105.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.9

File hashes

Hashes for lotrek_django_hvad-1.8.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d1fc6f32d100877814b60afcbefc977e07868900b938f86232e068c22d551861
MD5 f0600be20faef351db96a6217adb8b85
BLAKE2b-256 14b584ff9c75125a2a776631bb77154761a645619a5907c095c5f91917d4d298

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page