Skip to main content

django-parler-rest

Adding translation support to django-rest-framework.

Tests PyPI PyPI version License Coverage

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 django.utils.translation import gettext_lazy as _

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 __str__(self):
        return self.name

The model translations can be exposed as a separate 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')

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

Release files for django-parler-rest 2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-parler-rest 2.2
File Size Uploaded
django-parler-rest-2.2.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-parler-rest 2.2
File Interpreter ABI Platform
django_parler_rest-2.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 22.8 kB

Release files / django-parler-rest-2.2.tar.gz

Download URL django-parler-rest-2.2.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
58d0d7155964214dfbdb72783eff6a200c5e786e2d4f3cd74ea463d56cb10e9c
BLAKE2b-256 checksum
How to use checksums
0b769e6f7b0f370805fbb4dedfac7ff870afcef892c2d32ab1ef3192f85172f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / django_parler_rest-2.2-py2.py3-none-any.whl

Download URL django_parler_rest-2.2-py2.py3-none-any.whl
Size 11.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3cba9a47ec64771394e99ca49f9bef6f7dd76c1e39634fd634d2c1050fb9630a
BLAKE2b-256 checksum
How to use checksums
9264e4656794c743d0009425183b9cedfa79206c648902e6a41a758116784b8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

2.2 This release

2 release files

2.1

1 release file

2.0

2 release files

1.4.2

3 release files

1.4.1

2 release files

1.4

2 release files

1.3

2 release files

1.2.1

2 release files

1.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page