简体   繁体   English

Django Rest Framework 中的模型转换

[英]Model translation in Django Rest Framework

I'm developing an API with Django Rest Framework, and I'd need some models with a few fields that should support translation in multiple languages then, of course, serializers should have to retrieve the field with the expected language.我正在使用 Django Rest Framework 开发 API,我需要一些带有一些字段的模型,这些字段应该支持多种语言的翻译,当然,序列化程序应该使用预期的语言检索字段。 I've thought about two options: adding extra fields to the model (one field for language) or creating another model with all texts in every language.我考虑了两种选择:向模型添加额外的字段(一个字段用于语言)或创建另一个模型,其中包含每种语言的所有文本。 On the other hand, I've seen that there are some libraries such as django-modeltranslation that are intended to solve that issue, however, I'd like to know some opinions about them.另一方面,我已经看到有一些库,例如 django-modeltranslation 旨在解决该问题,但是,我想了解有关它们的一些意见。 What do you think?你怎么认为? What would you recommend to me?你会向我推荐什么?

Thank you very much非常感谢

As per documentation ,根据文档

Using in combination with django-rest-framework与 django-rest-framework 结合使用

When creating a new viewset , make sure to override get_queryset method, using queryset as a property won't work because it is being evaluated once, before any language was set.创建新的 viewset 时,请确保覆盖 get_queryset 方法,使用 queryset 作为属性将不起作用,因为它在设置任何语言之前被评估一次。

**** ****

So depending on your needs you can create a class inheriting from drf (for example ViewSet ), obtaining the language code from request and replacing the get_queryset method to filter by language.因此,根据您的需要,您可以创建一个继承自 drf 的类(例如ViewSet ),从请求中获取语言代码并替换get_queryset方法以按语言过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM