I wrote a Mixin for a DRF-API-View to dynamically support django-modeltranslation
See original GitHub issueI wrote a Mixin for the django-restframework API View to automatically deliver translations. Here is an example for German and English activated:
class AchievementSerializer(TranslatableSerializerMixin, serializers.ModelSerializer):
class Meta:
model = Achievement
fields = ('slug', 'title',)
When you now make a GET request to it, you will get this as a return:
{
"count": 6,
"current_page": 1,
"results": [{
"slug": "campaign",
"title": "Kampagne",
"title_de": "Kampagne",
"title_en": "Campaign"
}, {
"slug": "profile",
"title": "Profil",
"title_de": "Profil",
"title_en": "Profile"
}, {
"slug": "promote-project",
"title": "Projekt bekannt machen",
"title_de": "Projekt bekannt machen",
"title_en": "Promote Project"
}, {
"slug": "company",
"title": "Unternehmen",
"title_de": "Unternehmen",
"title_en": "Company"
}, {
"slug": "organization",
"title": "Organisation",
"title_de": "Organisation",
"title_en": "Organization"
}, {
"slug": "project",
"title": "Projekt",
"title_de": "Projekt",
"title_en": "Project"
}],
"links": {
"previous": null,
"next": null
}
}
You can also POST to it like normal.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:5
Top Results From Across the Web
django - Set created by modified by dynamically with mixin
Just use existing functionality https://docs.djangoproject.com/en/1.11/topics/db/models/#abstract-related-name class Trackable(models.
Read more >Using mixins with class-based views - Django documentation
Two central mixins are provided that help in providing a consistent interface to working with templates in class-based views. TemplateResponseMixin.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@iekadou How we can see the code of your mixin? Why didn’t you create a pull request?
Here’s a simple implementation of the above (mixin renamed). By default, it will provide all translations of the serializer fields. However, if the language get parameter is provided to the request as a comma delimited string, then only those specified translations will be returned.