在后台使用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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Admob不使用webview   Java Wicket表单:序列化对象类“myClassName”时出错   java googledriveapi更新文件与我的帐户管理员   java简单框架未知xml标记解析   java如何使用主类和用户获取Spark应用程序ID   java如何更改自定义属性的值?   java有没有办法在jMock中找到未使用的期望值?   gradle运行任务的java文档?   java通过使用数组来存储文本文件的行,通过覆盖来删除文本文件中的行   用于表达式语言注入的java利用负载   java IDEStyle程序运行   java在运行时启用/禁用Springws   爪哇芦苇。formatNumber NullPointException   java为什么我的代码无法检测两个动态实体之间的碰撞?