# django-parler-rest
**Adding translation support to [django-rest-framework](http://www.django-rest-framework.org/)**.
[![Tests](https://github.com/django-parler/django-parler-rest/actions/workflows/tests.yml/badge.svg)](https://github.com/django-parler/django-parler-rest/actions/workflows/tests.yml)
[![PyPI](https://img.shields.io/pypi/pyversions/django-parler-rest.svg)](https://pypi.python.org/pypi/django-parler-rest)
[![PyPI version](https://img.shields.io/pypi/v/django-parler-rest.svg)](https://pypi.python.org/pypi/django-parler-rest)
[![License](https://img.shields.io/pypi/l/django-parler-rest.svg)](https://pypi.python.org/pypi/django-parler-rest)
[![Coverage](https://img.shields.io/codecov/c/github/django-parler/django-parler-rest/master.svg)](https://codecov.io/github/django-parler/django-parler-rest?branch=master)
This package adds support for TranslatableModels from [django-parler](https://github.com/django-parler/django-parler)
to [django-rest-framework](http://www.django-rest-framework.org/).
## Installation
```shell
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](https://django-parler.readthedocs.io/en/latest/):
```python
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:
```python
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:
```json
{
"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`:
```shell
python setup.py test # install dependencies and run tests with coverage
```
Raw data
{
"_id": null,
"home_page": "https://github.com/edoburu/django-parler-rest",
"name": "django-parler-rest",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Diederik van der Boor",
"author_email": "opensource@edoburu.nl",
"download_url": "https://files.pythonhosted.org/packages/0b/76/9e6f7b0f370805fbb4dedfac7ff870afcef892c2d32ab1ef3192f85172f7/django-parler-rest-2.2.tar.gz",
"platform": null,
"description": "# django-parler-rest\n\n**Adding translation support to [django-rest-framework](http://www.django-rest-framework.org/)**.\n\n[![Tests](https://github.com/django-parler/django-parler-rest/actions/workflows/tests.yml/badge.svg)](https://github.com/django-parler/django-parler-rest/actions/workflows/tests.yml)\n[![PyPI](https://img.shields.io/pypi/pyversions/django-parler-rest.svg)](https://pypi.python.org/pypi/django-parler-rest)\n[![PyPI version](https://img.shields.io/pypi/v/django-parler-rest.svg)](https://pypi.python.org/pypi/django-parler-rest)\n[![License](https://img.shields.io/pypi/l/django-parler-rest.svg)](https://pypi.python.org/pypi/django-parler-rest)\n[![Coverage](https://img.shields.io/codecov/c/github/django-parler/django-parler-rest/master.svg)](https://codecov.io/github/django-parler/django-parler-rest?branch=master)\n\nThis package adds support for TranslatableModels from [django-parler](https://github.com/django-parler/django-parler)\nto [django-rest-framework](http://www.django-rest-framework.org/).\n\n\n## Installation\n\n```shell\npip install django-parler-rest\n```\n\n\n## Usage\n\n* First make sure you have django-parler_ installed and configured.\n* Use the serializers as demonstrated below to expose the translations.\n\nFirst configure a model, following the [django-parler documentation](https://django-parler.readthedocs.io/en/latest/):\n\n```python\nfrom django.db import models\nfrom django.utils.translation import gettext_lazy as _\n\nfrom parler.models import TranslatableModel, TranslatedFields\n\n\nclass Country(TranslatableModel):\n \"\"\"\n Country database model.\n \"\"\"\n\n country_code = models.CharField(_(\"Country code\"), unique=True, db_index=True)\n\n translations = TranslatedFields(\n name = models.CharField(_(\"Name\"), max_length=200)\n url = models.URLField(_(\"Webpage\"), max_length=200, blank=True)\n )\n\n class Meta:\n verbose_name = _(\"Country\")\n verbose_name_plural = _(\"Countries\")\n\n def __str__(self):\n return self.name\n```\n\nThe model translations can be exposed as a separate serializer:\n\n```python\nfrom rest_framework import serializers\nfrom parler_rest.serializers import TranslatableModelSerializer, TranslatedFieldsField\nfrom .models import Country # Example model\n\n\nclass CountrySerializer(TranslatableModelSerializer):\n translations = TranslatedFieldsField(shared_model=Country)\n\n class Meta:\n model = Country\n fields = ('id', 'country_code', 'translations')\n```\n\n**Note:** The `TranslatedFieldsField` can only be used in a serializer that inherits from\n`TranslatableModelSerializer`.\n\n\nThis will expose the fields as a separate dictionary in the JSON output:\n\n```json\n{\n \"id\": 528,\n \"country_code\": \"NL\",\n \"translations\": {\n \"nl\": {\n \"name\": \"Nederland\",\n \"url\": \"http://nl.wikipedia.org/wiki/Nederland\"\n },\n \"en\": {\n \"name\": \"Netherlands\",\n \"url\": \"http://en.wikipedia.org/wiki/Netherlands\"\n },\n \"de\": {\n \"name\": \"Niederlande\",\n \"url\": \"http://de.wikipedia.org/wiki/Niederlande\"\n }\n }\n}\n```\n\n## Contributing\n\nThis module is designed to be generic. In case there is anything you didn't like about it,\nor think it's not flexible enough, please let us know. We'd love to improve it!\n\nIf you have any other valuable contribution, suggestion or idea,\nplease let us know as well because we will look into it.\nPull requests are welcome too. :-)\n\n\n## Running tests\n\nTests are run with `py.test`:\n\n```shell\npython setup.py test # install dependencies and run tests with coverage\n```\n\n\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "Multilingual support for django-rest-framework",
"version": "2.2",
"project_urls": {
"Download": "https://github.com/edoburu/django-parler-rest/zipball/master",
"Homepage": "https://github.com/edoburu/django-parler-rest"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9264e4656794c743d0009425183b9cedfa79206c648902e6a41a758116784b8f",
"md5": "ab01c402fac9ceeb2674ffd12d801c5e",
"sha256": "3cba9a47ec64771394e99ca49f9bef6f7dd76c1e39634fd634d2c1050fb9630a"
},
"downloads": -1,
"filename": "django_parler_rest-2.2-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "ab01c402fac9ceeb2674ffd12d801c5e",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 11636,
"upload_time": "2022-05-04T14:05:30",
"upload_time_iso_8601": "2022-05-04T14:05:30.510864Z",
"url": "https://files.pythonhosted.org/packages/92/64/e4656794c743d0009425183b9cedfa79206c648902e6a41a758116784b8f/django_parler_rest-2.2-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0b769e6f7b0f370805fbb4dedfac7ff870afcef892c2d32ab1ef3192f85172f7",
"md5": "d811ba9be44d9cc0d775ad2c779e7d28",
"sha256": "58d0d7155964214dfbdb72783eff6a200c5e786e2d4f3cd74ea463d56cb10e9c"
},
"downloads": -1,
"filename": "django-parler-rest-2.2.tar.gz",
"has_sig": false,
"md5_digest": "d811ba9be44d9cc0d775ad2c779e7d28",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11117,
"upload_time": "2022-05-04T14:05:31",
"upload_time_iso_8601": "2022-05-04T14:05:31.972803Z",
"url": "https://files.pythonhosted.org/packages/0b/76/9e6f7b0f370805fbb4dedfac7ff870afcef892c2d32ab1ef3192f85172f7/django-parler-rest-2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-05-04 14:05:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "edoburu",
"github_project": "django-parler-rest",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "django-parler-rest"
}