/django-parler-rest

Translatable model support for django-rest-framework

Primary LanguagePythonApache License 2.0Apache-2.0

django-parler-rest

image

image

image

image

Adding translation support to django-rest-framework.

This package adds support for TranslatableModels from django-parler to django-rest-framework.

Installation

pip install django-parler-rest

Usage

  • First make sure you have django-parler installed and configured.
  • Use the serializers as demonstrated below to expose the translations.

First configure a model, following the django-parler documentation:

The model translations can be exposed as a separate serializer:

Note

The TranslatedFieldsField can only be used in a serializer that inherits from TranslatableModelSerializer.

This will expose the fields as a separate dictionary in the JSON output:

{
    "id": 528,
    "country_code": "NL",
    "translations": {
        "nl": {
            "name": "Nederland",
            "url": "http://nl.wikipedia.org/wiki/Nederland"
        },
        "en": {
            "name": "Netherlands",
            "url": "http://en.wikipedia.org/wiki/Netherlands"
        },
        "de" {
            "name": "Niederlande",
            "url": "http://de.wikipedia.org/wiki/Niederlande"
        }
    }
}

Contributing

This module is designed to be generic. In case there is anything you didn't like about it, or think it's not flexible enough, please let us know. We'd love to improve it!

If you have any other valuable contribution, suggestion or idea, please let us know as well because we will look into it. Pull requests are welcome too. :-)

Running tests

Tests are run with `py.test`:

python setup.py test  # install dependencies and run tests with coverage