Skip to main content

Model translation for Django without magic-inflicted pain

Project description

https://travis-ci.org/matthiask/django-translated-fields.svg?branch=master

Django model translation without magic-inflicted pain.

Installation and usage

After installing django-translated-fields into your Python environment all you have to do is define LANGUAGES and adding translated fields to your models:

from django.db import models
from django.utils.translation import gettext_lazy as _

from translated_fields import TranslatedField


class Question(models.Model):
    question = TranslatedField(
        models.CharField(_('question'), max_length=200),
    )
    answer1 = TranslatedField(
        models.CharField(_('answer 1'), max_length=200),
    )
    answer2 = TranslatedField(
        models.CharField(_('answer 2'), max_length=200),
    )
    answer3 = TranslatedField(
        models.CharField(_('answer 3'), max_length=200, blank=True),

        # Specific keyword arguments for fields, defaults to None.
        # For example, if you want to make the first language
        # mandatory:
        # {settings.LANGUAGES[0]: {'blank': False}},

        # The language is appended to the verbose_name of fields by
        # default . If you do not want this, add the following
        # argument:
        # verbose_name_with_language=False,

        # The default is to take [lang[0] for lang in LANGUAGES]
        # languages=None,

        # The default implementation returns the attribute related
        # to ``translation.get_language``. Pass a callable receiving
        # the translated field's name and returning a callable which
        # has ``self`` as its only argument.
        # attrgetter=None,
    )

    def __str__(self):
        return self.question

Model fields are automatically created from the field passed to TranslatedField, one field per language. The TranslatedField instance itself is replaced with a property which returns the current language’s attribute. There are no default values or fallbacks, only a wrapped attribute access.

If model field creation is not desired, you may also use the translated_attributes class decorator. This only creates the attribute getter property:

from translated_fields import translated_attributes

@translated_attributes('attribute', 'anything', ...)
class Test(object):
    attribute_en = 'some value'
    attribute_de = 'some other value'

There is no support for automatically referencing the current language’s field in queries or automatically adding fields to admin fieldsets and whatnot. The code required for these features isn’t too hard to write, but it is hard to maintain down the road which contradicts my goal of writing low maintenance software. Still, feedback and pull requests are very welcome! Please run the style checks and test suite locally before submitting a pull request though – all that this requires is running tox.

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

django-translated-fields-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_translated_fields-0.2.0-py2.py3-none-any.whl (4.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-translated-fields-0.2.0.tar.gz.

File metadata

File hashes

Hashes for django-translated-fields-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8c1542eeac5aeb44ed467201a0968f6ba0a5dedc3e1fe69a2e611e8a0009efd1
MD5 f7613c3b7ebbaadd1f5fe284c1abaf2a
BLAKE2b-256 d322ddcdc67e69d0e4fbe55e05095ea945c87f8561df521eee3e6e8499225fdf

See more details on using hashes here.

File details

Details for the file django_translated_fields-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_translated_fields-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 aec246bebed575a34f6482cc542a01f0679b7ef45ce14c175c7c18040f26e748
MD5 32a08a3c8cc499c76c49b9c52588b714
BLAKE2b-256 b82838c069a6c351ede4446bd3087ad97eb010a712f3bbfeac2e80fa2d26603b

See more details on using hashes here.

Supported by

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