django-commons / drf-excel

An XLSX spreadsheet renderer for Django REST Framework.
BSD 3-Clause "New" or "Revised" License
217 stars 40 forks source link

fix support django lazy property #76

Closed BilityLeu closed 1 year ago

BilityLeu commented 1 year ago

Thank you for developing such an excellent plugin. I have found that when encountering django lazy loading, an error will be reported. The following example can reproduce this problem:

from django.db import models
from django.utils.translation import gettext_lazy as _
class MyModel(models.Model):
    STATUS_CHOICES = (
        ('Created', _('Created')),
        ('Processing', _('Processing')),
        ('Finished', _('Finished')),
    )
    status = models.CharField(choices=STATUS_CHOICES)
    ...

from rest_framework import serializers
class MySerializers(serializers.ModelSerializer):
    status_display = serializers.CharField(source='get_status_display')

    class Meta:
        model = MyModel
        fields = ('status', 'status_display')

Only use gettext_ Lazy only has this issue, and gettext does not. I have analyzed that the reason for the problem is that lazy returned a obj of __ proxy__(a subclass of Promise) type , which is not a common data type.

I tried to fix this issue and submitted a merge request, which may not be elegant, but at least it works.