Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

rename the get_field_display in django serializer

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,
        )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading