pythondjangodjango-rest-frameworkdjango-hvad

Django rest framework, translate model with django-hvad


I have model Product:

class Product(TranslatableModel):
    name = models.CharField(max_length=255, unique=True)

    translations = TranslatedFields(
        description=models.TextField(),
    )

and in administration on product detail I have tabs with languages. For example tabs EN, CZ, each includes disctiption. So PUT request looks like:

{
    'product': '1',
    'id': 1,
    'name': 'Name', 
    'translations': {
        'cz': {'desctiption': 'Description cz'},
        'en': {'desctiption': 'Description en'}
    }
}

I founded in django-hvad TranslationsMixin that allows me to do that request.

in serializers I have:

class ProductTranslationSerializer(serializers.ModelSerializer):
    class Meta:
        exclude = ['description']


class ProductSerializer(TranslationsMixin, serializers.ModelSerializer):
    class Meta:
        model = Product
        translations_serializer = ProductTranslationSerializer
        fields = (
            'name', 
            'description',
        )

Question is how will looks ModelViewSet for this request? Can I choose language like 'language_code=en', filter that query and get something like:

[
    {
        id: 1
        name: "name"
        descritpion: "descritpion"
    },
    ....
]

Thank you!


Solution

  • the most likely way i achieve is:

    models.py

    class Product(TranslatableModel):
        category = models.ForeignKey('product.ProductCategory',
                                     related_name='product_category',
                                     null=True,
                                     on_delete=models.SET_NULL,
                                     verbose_name=u'category')
        cover = models.ImageField(upload_to=product_cover,
                                  null=True,
                                  verbose_name=u'cover')
        translations = TranslatedFields(
            title=models.CharField(max_length=100,
                                   null=True,
                                   verbose_name=u'title'),
            summary=models.TextField(null=True,
                                     verbose_name=u'summary'),
            model=models.CharField(max_length=255,
                                   null=True,
                                   blank=True,
                                   verbose_name=u'model'),
            price=models.DecimalField(default=0.00,
                                      max_digits=10,
                                      decimal_places=2,
                                      blank=True,
                                      validators=[MinValueValidator(0)],
                                      verbose_name=u'price'),
            content=models.TextField(verbose_name=u'content'),
            publish_time=models.DateTimeField(default=timezone.now,
                                              verbose_name=u'发布publish_time')
    
        )
        view_times = models.IntegerField(default=0,
                                         verbose_name=u'view_times ')
    

    views.py

    class ProductViewSet(ModelViewSet):
        serializer_class = ProductListSerializer
    
        def get_queryset(self):
            if 'language_code' in self.request.GET:
                language_code = self.request.GET.get('language_code')
                queryset = Product.objects.language(language_code).order_by('-id')
            else:
                queryset = Product.objects.language().order_by('-id')
            return queryset
    

    serializers.py

    class ProductCategorySerializer(TranslatableModelSerializer):
        class Meta:
            model = ProductCategory
            fields = '__all__'
    
    
    
    class ProductListSerializer(TranslatableModelSerializer):
        category = ProductCategorySerializer(read_only=True)
    
        class Meta:
            model = Product
            exclude = ['is_abandon', 'content', ]
    

    urls.py

       from rest_framework import routers
       router = routers.DefaultRouter()
       router.register(r'product', ProductViewSet, base_name='api-product')
       ...
    

    result: http://192.168.1.108/api/product/?language_code=zh-hans you get:

    {
        "count": 1,
        "page_num": 1,
        "page_no": 1,
        "next": "",
        "previous": "",
        "results": [
            {
                "id": 2,
                "category": {
                    "id": 2,
                    "create_time": "2017-08-10 16:49:41",
                    "update_time": "2017-08-18 08:56:02",
                    "name": "测试",
                    "language_code": "zh-hans"
                },
                "create_time": "2017-08-18 08:53:46",
                "update_time": "2017-08-18 08:56:28",
                "cover": "http://192.168.1.108/media/product/20170818-085346-518_59.jpg",
                "view_times": 0,
                "title": "标题",
                "summary": "简介",
                "model": null,
                "price": "90.00",
                "publish_time": "2017-08-18 08:53:00",
                "language_code": "zh-hans"
            }
        ]
    }
    

    http://192.168.1.108/api/product/?language_code=en you get:

    {
        "count": 1,
        "page_num": 1,
        "page_no": 1,
        "next": "",
        "previous": "",
        "results": [
            {
                "id": 2,
                "category": {
                    "id": 2,
                    "create_time": "2017-08-10 16:49:41",
                    "update_time": "2017-08-18 08:56:02",
                    "name": "测试",
                    "language_code": "zh-hans"
                },
                "create_time": "2017-08-18 08:53:46",
                "update_time": "2017-08-18 09:00:23",
                "cover": "http://192.168.1.108/media/product/20170818-085346-518_59.jpg",
                "view_times": 0,
                "title": "title",
                "summary": "summary",
                "model": "model",
                "price": "91.00",
                "publish_time": "2017-08-18 08:56:04",
                "language_code": "en"
            }
        ]
    }
    

    this way fk will not change language,if you want fk change language too,use: urls.py

    urlpatterns += i18n_patterns(
        '''
        url(r'^api/', include(router.urls)),
        prefix_default_language=False,
    )
    

    change language by

     http://192.168.1.108/zh-hans/api/product/
    

    to

     http://192.168.1.108/en/api/product/
    

    your will get:

    {
        "count": 1,
        "page_num": 1,
        "page_no": 1,
        "next": "",
        "previous": "",
        "results": [
            {
                "id": 2,
                "category": {
                    "id": 2,
                    "create_time": "2017-08-10 16:49:41",
                    "update_time": "2017-08-18 08:56:02",
                    "name": "test",
                    "language_code": "en"
                },
                "create_time": "2017-08-18 08:53:46",
                "update_time": "2017-08-18 09:00:23",
                "cover": "http://192.168.1.108/media/product/20170818-085346-518_59.jpg",
                "view_times": 0,
                "title": "title",
                "summary": "summary",
                "model": "model",
                "price": "91.00",
                "publish_time": "2017-08-18 08:56:04",
                "language_code": "en"
            }
        ]
    }