Multilingual support for django-rest-framework
Project description
django-parler-rest
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. :-)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-parler-rest-1.2.1.tar.gz
.
File metadata
- Download URL: django-parler-rest-1.2.1.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a8e0f0736712fd71dfbc49daa1b8808399079a9c3e8adcf26fe86915108bd09 |
|
MD5 | f2e90cbd0955e839a5ce14e2c19b8b8c |
|
BLAKE2b-256 | a863d8171c1e0663e0687a9ef148ff823325fceb18c0efd6b2d1a6df2dc27f11 |
Provenance
File details
Details for the file django_parler_rest-1.2.1-py2-none-any.whl
.
File metadata
- Download URL: django_parler_rest-1.2.1-py2-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3395c5360ab0eaa3c176d801e62967bafca38e0bb120efdbea45e5a3badcbdff |
|
MD5 | f7c98ed1a85b32d10eafc1e17f31aeae |
|
BLAKE2b-256 | c14cea525d890d41d50151a4e5ae6702480d3b9e0eeee43b794d4e9a692041c8 |