I have a django model that looks like this:
class AcademicProgramsManager(models.Manager):
def get_by_natural_key(self, acad_program_id, program_title, required_credits):
return self.get(acad_program_id = acad_program_id, program_title = program_title, required_credits = required_credits)
class AcademicPrograms(models.Model):
objects = AcademicProgramsManager()
acad_program_id = models.IntegerField(primary_key=True)
acad_program_category = models.ForeignKey(AcademicProgramCategories)
acad_program_type = models.ForeignKey(AcademicProgramTypes)
acad_program_code = models.CharField(max_length=25)
program_title = models.CharField(max_length=64)
required_credits = models.IntegerField()
min_gpa = models.FloatField()
description = models.CharField(max_length=1000)
def natural_key(self):
return (self.acad_program_id, self.program_title, self.required_credits)
class StudentAcademicPrograms(models.Model):
student = models.ForeignKey(Students)
academic_program = models.ForeignKey(AcademicPrograms)
credits_completed = models.IntegerField()
academic_program_gpa = models.FloatField()
primary_program = models.BooleanField()
This is my serializers.py file:
from studentapp.models import AcademicPrograms, AcademicProgramsManager, StudentAcademicPrograms
from rest_framework import serializers
class StudentAcademicProgramsSerializer(serializers.ModelSerializer):
class Meta:
model = StudentAcademicPrograms
fields = ('credits_completed','academic_program_gpa','primary_program')
class AcademicProgramsSerializer(serializers.ModelSerializer):
studentacademicprograms = StudentAcademicProgramsSerializer(many = True)
class Meta:
model = AcademicPrograms
fields = ('acad_program_id','program_title','required_credits','studentacademicprograms')
My api.py file looks like this:
from studentapp.models import AcademicPrograms, AcademicProgramsManager, StudentAcademicPrograms
from studentapp.serializers import StudentAcademicProgramsSerializer, AcademicProgramsSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
class StudentAcademicProgramsList(APIView):
def get(self, request, format=None):
studentacademicprograms = StudentAcademicPrograms.objects.all()
serialized_studentacademicprograms = StudentAcademicProgramsSerializer(studentacademicprograms, many=True)
return Response(serialized_studentacademicprograms.data)
class StudentAcademicProgramsDetail(APIView):
def get_onjects(self, pk):
try:
return StudentAcademicPrograms.object.get(pk=pk)
except StudentAcademicPrograms.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
studentacademicprogram = self.get_object(pk)
serialized_studentacademicprogram = StudentAcademicProgramsSerializer(studentacademicprogram)
return Response(serialized_studentacademicprogram.data)
When i call my StudentAcademicProgramsSerializer
in the urls, i get an output like this:
[
{
"credits_completed": 32,
"academic_program_gpa": 3.7,
"primary_program": true
},
{
"credits_completed": 32,
"academic_program_gpa": 3.4,
"primary_program": false
}
]
But i want an output that looks like this:
[
{
"pk": 1,
"fields": {
"academic_program": [
"acad_program_id": 124,
"program_title": "Associate in Arts Degree",
"required_credits": 60
],
"credits_completed": 32,
"primary_program": true,
"student": 1206,
"academic_program_gpa": 3.7
}
},
{
"pk": 2,
"fields": {
"academic_program": [
"acad_program_id": 123,
"program_title": "Associate in Sciences Degree",
"required_credits": 60
],
"credits_completed": 32,
"primary_program": false,
"student": 1206,
"academic_program_gpa": 3.4
}
}
]
How do i achieve it?