Since I am new to this DRF I am facing some difficulties creating this JSON. I have created an API endpoint and the output of that API endpoint is like this now as shown below
"meta": {
"limit": 1,
"page_count": 1,
"total_count": 1
},
"results": {
"id": 1234567,
"curriculum": {
"ES Math": [
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 438,
"product_title": "Test1",
"ratings": [
{
"source": "A",
"report_url": "********",
"Org1_rating": [
{
"name": "green_alignment_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
},
{
"name": "green_usability_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
}
],
"Org2_rating": [
{
"name": "Overall",
"value": null,
"label": "Tier 1: Exemplifies Quality",
"label_color": null
}
]
}
]
},
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 2085,
"product_title": "Test2",
"ratings": [
{
"source": "A",
"report_url": "********",
"Org1_rating": [
{
"name": "green_alignment_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
},
{
"name": "green_usability_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
}
],
"Org_rating2": "null"
}
]
}
]
}
}
}
But I want the output to be in this format below
{
"meta": {
"limit": 1,
"page_count": 1,
"total_count": 1
},
"results": {
"id": 1234567,
"curriculum": {
"ES Math": [
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 438,
"product_title": "Test1",
"ratings": [
{
"review_org": "Org1",
"review_url": "url",
"review_items": [
{
"name": "green_alignment_ratings",
"value": 14,
"label": "Meets Expectations",
"label_color": "Green"
},
{
"name": "green_usability_ratings",
"value": 34,
"label": "Green",
"label_color": 38
}
]
},
{
"review_org": "Org2",
"review_url": "url",
"review_items": [
{
"name": "Overall",
"value": null,
"Label": "Tier I, Exemplifies quality",
"scale": null
}
]
}
]
},
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 2085,
"product_title": "Test2",
"ratings": [
{
"review_org": "Org1",
"review_url": "url",
"review_items": [
{
"name":"green_alignment_ratings",
"value": 14,
"label": "Meets Expectations",
"label_color": "Green"
},
{
"name":"green_usability_ratings",
"value": 34,
"label": "Meets Expectations",
"label_color": "Green"
}
]
},
{
"review_org": "Org2",
"review_url": "url",
"review_items": []
}
]
}
]
}
}
}
And I tried something with the serializer below but this is yeilding some different JSON only.
class CurriculumSerializer(ModelSerializer):
grade_level = serializers.CharField(source='grade_level_dim')
subject_abbr = serializers.CharField(source='subject_abbr_dim')
product_id = serializers.IntegerField(source='dim_id')
product_title = serializers.CharField(source='title_dim')
ratings = serializers.SerializerMethodField()
class Meta:
model = Table2
fields = [
'grade_level',
'subject',
'subject_abbr',
'product_id',
'product_title',
'ratings'
]
def get_ratings(self,obj):
queryset = Table2.objects.all()
c = queryset.filter(id=obj.id, title_dim = obj.title_dim, ratings_source__isnull=False).distinct('id',)
if c.exists():
serializer = RatingsSerializer(c, many=True)
return serializer.data
else:
data = 'null'
return data
class RatingsSerializer(ModelSerializer):
review_items = serializers.SerializerMethodField()
Louisiana_rating = serializers.SerializerMethodField()
class Meta:
model = Table3
fields = ['source','report_url','review_items','Louisiana_rating']
def get_review_items(self, obj):
queryset = Table3.objects.all()
c = queryset.filter(source__iexact = 'Org1', title = obj.title_dim, grade_level = obj.grade_level, subject_abbr = obj.subject_abbr_dim)
# print(c.query)
if c.exists():
serializer = reportSerializer(c, many=True)
return serializer.data
else:
data = 'null'
return data
def get_Louisiana_rating(self, obj):
queryset = Table3.objects.all()
c = queryset.filter(source__iexact = 'Org2', title = obj.title_dim, grade_level = obj.grade_level, subject_abbr = obj.subject_abbr_dim)
if c.exists():
serializer = reportSerializer(c, many=True)
return serializer.data
else:
data = 'null'
return data
class reportSerializer(ModelSerializer):
class Meta:
model = Table3
fields = ['name',
'value',
'label',
'label_color']
class DistrictDetailSerializer(ModelSerializer):
curriculum = serializers.SerializerMethodField()
class Meta:
model = Table1
exclude = ('finance_total', 'revenue_total')
def get_curriculum(self, obj):
queryset_list = Table2.objects.all()
c = queryset_list.filter(id=obj.id)\
.distinct('col1','col2')
if c.exists():
serializer = CurriculumSerializer(c, many=True)
curriculum_map = {}
for d in serializer.data:
key = f"{d['grade_level']} {d['subject_abbr']}"
if key in curriculum_map:
curriculum_map[key].append(d)
else:
curriculum_map[key] = [d, ]
return curriculum_map
else:
data = 'null'
return data
The table3 has all details for ratings like source, report_url, name,label,value,label_colour I want this values as show in the JSON below.
This error usually occurs if the user account was not given/assigned to the Virtual Machine User Login role on the VMs. Please check this https://learn.microsoft.com/en-us/azure/active-directory/devices/howto-vm-sign-in-azure-ad-windows#azure-role-not-assigned to assign required roles and try login.