Skip to content

fix support django lazy property#76

Merged
FlipperPA merged 1 commit intodjango-commons:mainfrom
BilityLeu:bugfix/proxy-type-value
Jun 23, 2023
Merged

fix support django lazy property#76
FlipperPA merged 1 commit intodjango-commons:mainfrom
BilityLeu:bugfix/proxy-type-value

Conversation

@BilityLeu
Copy link
Copy Markdown

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.

@FlipperPA FlipperPA merged commit f3ac655 into django-commons:main Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants