I thought it could be a nice idea to provide the Choices of a field with the api. Something like that:
{
"kind": "first-type",
"kind_choices": {
"first-type": "First",
"second-type": "Second"
}
}
But my Model Choices are translatable:
Model(models.Model):
KIND = (("first-type", _("Translate First"), ("second-type", _("Translate Second")
and when I try to put in the serializer
Serializer(serializers.HyperLinkedModelSerializer):
kind_choices = serializers.Field(source="KIND")
it dies with the error
<django.utils.functional.__proxy__ object at 0x7fac6c074290> is not JSON serializable
It means of course that the JSON serializer doesn't call the unicode method on the translatable fields.
With a custom method on the serializer I managed to solve this in a rather crude way:
def get_kind_choices(self, obj):
render = {}
for key, value in obj.KIND:
render[key] = unicode(value)
return render
I have a couple question:
1. Is it a good idea to provide the possible Choices through the api? There's a better way to do it?
2. Provided the first question is "Yes, you are doing fine, good job!". What's the best approach to solve this? Should I subclass the JSON Renderer? Create a Custom Field? Is it worth creating a patch to fix the JSONRenderer itself?
Thanks for your time!