0

models.py

class Project(models.Model):
    project_id = models.CharField(max_length=50,default=uuid.uuid4, editable=False, unique=True, primary_key=True)
    org = models.ForeignKey(Organisation, on_delete=models.CASCADE, related_name='org_project',null=True)
    product = models.ManyToManyField(Product,related_name='product_project')
    client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='client_project')
    project_name = models.CharField(unique=True,max_length=100)
    project_code = models.CharField(max_length=20)
    project_cost = models.IntegerField(null=True)
    currency_type = models.CharField(max_length=100, choices=CURRENCY_CHOICES, default='Indian Rupee')
    project_head = models.ForeignKey(User_Master, on_delete=models.CASCADE, related_name='project_head',null=True)
    project_manager = models.ForeignKey(User_Master, on_delete=models.CASCADE, related_name='project_manager',null=True)
    project_user = models.ManyToManyField(User_Master,related_name='project_user')
    description = models.TextField(max_length=500)
    start_date = models.DateField()
    end_date = models.DateField()
    techstack = models.ManyToManyField(Techstack,related_name='techstack_project')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    is_active = models.IntegerField(default=0, choices=STATUS_CHOICES)

views.py

from djmoney.settings import CURRENCY_CHOICES

class CurrencyList(APIView):
    renderer_classes = (CustomRenderer,)

    def get(self, request, *args, **kwargs):
        return Response(CURRENCY_CHOICES, status=status.HTTP_200_OK)

I was Trying to get the Currency code list in a response so that the data can be sent to the frontend as a dropdown list.

When I Used the above django money package I am getting an response as

{
    "status": "success",
    "code": 200,
    "data": [
        [
            "XUA",
            "ADB Unit of Account"
        ],
        [
            "AFN",
            "Afghan Afghani"
        ],
        [
            "AFA",
            "Afghan Afghani (1927–2002)"
        ],
        ....
],
    "message": null
}

But I need to Get the response as list of json inside the data not as the list of arrays,

{
    "status": "success",
    "code": 200,
    "data": [
        {
            "shortname": "XUA",
            "fullname": "ADB Unit of Account"
        },
        {
            "shortname":"AFN",
            "fullname":"Afghan Afghani"
        },
        {
            "shortname":"AFA",
            "fullname":"Afghan Afghani (1927–2002)"
        },
        ....
],
    "message": null
}

Is it possible to modify the response for this package? or is there any other way to achieve the end goal response by any other method?

0

2 Answers 2

1

You just need to adjust your response in the following way:

from djmoney.settings import CURRENCY_CHOICES

class CurrencyList(APIView):
    renderer_classes = (CustomRenderer,)

    def get(self, request, *args, **kwargs):
        return Response(
            [{'shortname': short, 'fullname': full} for short, full in CURRENCY_CHOICES],
            status=status.HTTP_200_OK,
    )
Sign up to request clarification or add additional context in comments.

Comments

0

try this

from djmoney.settings import CURRENCY_CHOICES

class CurrencyList(APIView):
    renderer_classes = (CustomRenderer,)

    def get(self, request, *args, **kwargs):
        return Response([dict(zip(['shortname', 'fullname'], c)) for c in CURRENCY_CHOICES], status=status.HTTP_200_OK)

python console

>>> data = [
...         [
...             "XUA",
...             "ADB Unit of Account"
...         ],
...         [
...             "AFN",
...             "Afghan Afghani"
...         ],
...         [
...             "AFA",
...             "Afghan Afghani (1927–2002)"
...         ]
         ]
>>> 
>>> [dict(zip(['shortname', 'fullname'], c)) for c in data ]
[{'shortname': 'XUA', 'fullname': 'ADB Unit of Account'}, {'shortname': 'AFN', 'fullname': 'Afghan Afghani'}, {'shortname': 'AFA', 'fullname': 'Afghan Afghani (1927–2002)'}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.