
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Adding translation support to django-rest-framework.
This package adds support for TranslatableModels from django-parler to django-rest-framework.
pip install django-parler-rest
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"
}
}
}
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. :-)
Tests are run with py.test
:
python setup.py test # install dependencies and run tests with coverage
FAQs
Multilingual support for django-rest-framework
We found that django-parler-rest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.