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

Metadata

Release files for jst-parler-rest 1.0.1

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

Source distribution (sdist)

Source distribution for jst-parler-rest 1.0.1
File Size Uploaded Provenance Actions
jst_parler_rest-1.0.1.tar.gz 11.3 kB Without provenance Details for jst_parler_rest-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jst-parler-rest 1.0.1
File Interpreter ABI Platform Actions
jst_parler_rest-1.0.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for jst_parler_rest-1.0.1-py2.py3-none-any.whl

Total release size: 23.2 kB

Release files / jst_parler_rest-1.0.1.tar.gz

Download URL jst_parler_rest-1.0.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a56176241dcad5d8145bc5d5e7be32a4693c074c0f28c2900c1b6df0b8b0db26
BLAKE2b-256 checksum
How to use checksums
9e744bf3a7470ce99107e3781627018d9dc54eb2066d68f6f953a2cb4e51c0c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / jst_parler_rest-1.0.1-py2.py3-none-any.whl

Download URL jst_parler_rest-1.0.1-py2.py3-none-any.whl
Size 11.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
714e110d6d2dbbf90be1d909cb44d70315f7427c51a29413bd908a2b0f3a47b3
BLAKE2b-256 checksum
How to use checksums
a21ce3cbb2d0cb4bb9ff6ae8673c8f4d28e940d9a5fe2c5b5f1cc97f183e5cfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Security for jst-parler-rest 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

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