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),
    )

    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'

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.1.0.tar.gz (4.4 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.1.0-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

File hashes

Hashes for django-translated-fields-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6cc1b7183b557aa2291b8ec654abe3cd94a49a93cbde34baa5ceaa5d1c131da8
MD5 752cc4c1872a58dbb46e2336a9c9b2ac
BLAKE2b-256 dc744b6773939647ba2e4cb1e577ab38eb88e179bb1a774548905fa1e04777c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_translated_fields-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7fb9e5bf2e036db284e4abc96ae996c3d769aba99a9678d94ea733abd48f2307
MD5 45e7752fc7aea83043447914c6b6455c
BLAKE2b-256 becd5bcab03b1550f112ddf793507369fa848667e13f6105be7935fca0b26b92

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