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?
CodePudding user response:
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,
)
CodePudding user response:
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)'}]