Skip to main content

Multilingual support for django-rest-framework

Project description

django-parler-rest

https://travis-ci.org/edoburu/django-parler-rest.svg?branch=master https://img.shields.io/pypi/v/django-parler-rest.svg https://img.shields.io/pypi/dm/django-parler-rest.svg https://img.shields.io/badge/wheel-yes-green.svg https://img.shields.io/pypi/l/django-parler-rest.svg https://img.shields.io/codecov/c/github/edoburu/django-parler-rest/master.svg

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:

from django.db import models
from parler.models import TranslatableModel, TranslatedFields


class Country(TranslatableModel):
    """
    Country database model.
    """

    country_code = models.CharField(_("Country code"), unique=True, db_index=True)

    translations = TranslatedFields(
        name = models.CharField(_("Name"), max_length=200)
        url = models.URLField(_("Webpage"), max_length=200, blank=True)
    )

    class Meta:
        verbose_name = _("Country")
        verbose_name_plural = _("Countries")

    def __unicode__(self):
        return self.name

The model translations can be exposed as a seperate serializer:

from rest_framework import serializers
from parler_rest.serializers import TranslatableModelSerializer, TranslatedFieldsField
from .models import Country   # Example model


class CountrySerializer(TranslatableModelSerializer):
    translations = TranslatedFieldsField(shared_model=Country)

    class Meta:
        model = Country
        fields = ('id', 'country_code', 'translations')

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

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-parler-rest-1.4.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

django_parler_rest-1.4-py2-none-any.whl (9.4 kB view details)

Uploaded Python 2

File details

Details for the file django-parler-rest-1.4.tar.gz.

File metadata

File hashes

Hashes for django-parler-rest-1.4.tar.gz
Algorithm Hash digest
SHA256 d545ec503adeca010eba7fe37ba57f4c9665da381023e235489049992ca5bc5b
MD5 93f26cebf5f9c05dd91f94d75a77d94f
BLAKE2b-256 0e9fbd1a8f3ecef3d7e810f059793ee7c34149a3a87eea371c0fd637f8cc1036

See more details on using hashes here.

Provenance

File details

Details for the file django_parler_rest-1.4-py2-none-any.whl.

File metadata

File hashes

Hashes for django_parler_rest-1.4-py2-none-any.whl
Algorithm Hash digest
SHA256 1bdc132c6514d6e86bc86f93b4b261b60256e4184c33493f5ce4190b44b19ed2
MD5 91a99eee4c4eed772f97c3f4e8d571c4
BLAKE2b-256 c6754b81dfcc2678c98db194adc140a6e2e3914f5f3e363eba63f71de2f273a5

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page