Model translation for Django without magic-inflicted pain
Project description
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'
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-translated-fields-0.1.1.tar.gz.
File metadata
- Download URL: django-translated-fields-0.1.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e33c749bbb50ccf66f0c0411aa93c94247622a9c00ec8c6fc5b8777330fe7f6
|
|
| MD5 |
80bc0dead2253a876e43eaecf802eb00
|
|
| BLAKE2b-256 |
c103d4cedebf861eded2bc137f052a444a0dc73bd7cca154dca6b9c43d0b0497
|
File details
Details for the file django_translated_fields-0.1.1-py2.py3-none-any.whl.
File metadata
- Download URL: django_translated_fields-0.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d3952bb01744a826e4fb3ddc0293ef5328a521143baea26b9e975cbdda7b05f
|
|
| MD5 |
f912164b061125a97bb6cefc6c8f1109
|
|
| BLAKE2b-256 |
2a5705da12353d214a2424fb28049b7458727d0e913bc9c28ec6ce684f308e79
|