在后台使用postgresql的jsonb字段的内容转换框架
nece的Python项目详细描述
内克?
简介
。图::https://raw.githubusercontent.com/tatterdemalion/django-nece/master/images/nece.png :alt:nece
使用PostgreSQL的JSONB字段的“内容转换框架”。它
只需从名为
translations
。
为什么?
You might ask why you should use django-nece since there are other, and
more mature content translation frameworks like `django-hvad`_ and
`django-modeltranslation`_. Both of them are good in some ways, worst in
others.
For instance, it is very hard for ``django-hvad`` users to get default
language if there is no corresponding translation for an object. And it
holds translated values in a different table, so every translation query
results in another hit to the database.
On the other hand ``django-modeltranslation`` adds multiple additional
fields for multiple languages. The number of fields inceases by the
number of languages you need to support. At the end it becomes a huge
chunk of an object if you need to add more than 20 languages.
``nece?`` more or less works like the latter one with an important
difference. It uses Postgresql’s new ``JSONB`` field to hold translation
information. And overrides the original one on query.
Dependencies
------------
::
postgresql >= 9.4.5
Django >= 1.9
psycopg2 >= 2.5.4
Installation
------------
via pypi:
::
pip install nece
通过setup.py
::
python setup.py install
Usage
-----
Lets say we have a model called ``Fruit``:
::
from nece.models import TranslationModel
class Fruit(TranslationModel):
name = CharField(max_length=255)
def __str__(self):
return self.name
class Meta:
translatable_fields = ('name',)
``TranslationModel`` adds a jsonb field to this table and sets
translations in a notation like the one below:
::
{u'de_de': {u'name': u'Apfel'},
u'tr_tr': {u'name': u'elma'}}
When we need the German translation we can simply choose the language
and get the attribute as usual:
::
>> f = Fruit.objects.get(name='apple')
>> print(f.name)
apple
>> f.language('de_de')
>> print(f.name)
Apfel
You can also filter out the ones containing any language translation:
::
>> Fruit.objects.all()
[<Fruit: apple>, <Fruit: pear>, <Fruit: banana>]
>> Fruit.objects.language('tr_tr')
[<Fruit: elma>, <Fruit: armut>] # there is no translation for banana
>> Fruit.objects.language_or_default('tr_tr')
[<Fruit: elma>, <Fruit: armut>, <Fruit: banana>]
>> Fruit.objects.language('tr_tr').filter(name='elma')
[<Fruit: elma>]
>> Fruit.objects.language('tr_tr').get(name='elma')
<Fruit: elma>
Updating translations
::
>> fruit._language_code
tr_tr
>> fruit.name
elma
>> fruit.translate(name='armut').save()
>> fruit.name
armut
>> fruit.language('en')
>> fruit.translate('it_it', name='pera')
>> fruit.language('it_it')
>> fruit.name
pera
设置
翻译默认值
Default language code. Default value: ```en_us```
TRANSLATIONS_MAP
~~~~~~~~~~~~~~~~
Shortcuts for ```languagecode_countrycode``` notation.
Example:
::
TRANSLATIONS_MAP = {
"en": "en_us",
"tr": "tr_tr",
"ar": "ar_sy",
"bg": "bg_bg",
"cs": "cs_cz",
"da": "da_dk",
...
}
Default:
::
{'en': 'en_us'}
TRANSLATIONS_FALLBACK
如果缺少翻译,将使用回退语言。
示例:
:: 翻译后退={ 'fr_ca':['fr_fr'], 'en'u us':['en'u gb'], }
管理面板
使用TranslatableModelAdmin
作为漂亮的json编辑器(由django-admin-json-editor
提供支持)。
。图::https://raw.githubusercontent.com/tatterdemalion/django-nece/master/images/admin.png :alt:nece
示例:
。代码块::python
设置.py
已安装的应用程序=[ … 'django_admin_json_编辑器', … ]
管理py
从nece.admin导入translatablemodeladmin
类placeadmin(translatablemodeladmin): 列表显示=(“…”)
贡献者和感谢
Erkan Ay
Ayman Khalil
Gönül Sabah
Faruk Rahmet
Mathieu Richardoz
Marc Hertzog
Alexey Kotenko
Change Log
。_ django-hvad:https://github.com/kristianoellegaard/django-hvad ……_ Django-ModelTranslation:https://github.com/deschler/django-modeltranslation ……_埃尔坎埃:https://github.com/erkanay ……_艾曼·哈利勒:https://github.com/aymankh86 ……_格努尔沙巴:https://github.com/gonulsabah ……_法鲁克·拉赫梅特:https://github.com/farukrahmet ……_马修·理查多兹:https://github.com/metamatik ……_马克·赫特佐格:https://github.com/kemar ……_阿列克谢·科滕科:https://github.com/k0t3n ……_更改日志:https://github.com/tatterdemalion/django-nece/blob/master/CHANGELOG.md ……_ django-admin-json-editor:https://github.com/abogushov/django-admin-json-editor