Home > Blockchain >  rename the get_field_display in django serializer
rename the get_field_display in django serializer

Time:02-22

i am trying to use the get_field_display function in Django in my serializer but I want to rename it to something else(consider it worker_role). what should I do?

class AdminUnsubscriberListSerializer(serializers.ModelSerializer):
    worker_role= "get_role_display"

and the model is :

 class Worker(models.Model):
     ROLE_OPTIONS= [
    ("AD", _("admin")),
    ("WO", _("Worker")),]

        role= models.CharField(
            choices=ROLE_OPTIONS,
            null=True,
            blank=True,
            default=None,
        )

CodePudding user response:

You could write it as worker_role = serializers.CharField(source="get_role_display")

also it supports nested lookups for example

worker_role = serializers.CharField(source="worker.get_role_display")

  • Related