Reputation: 2559
I am trying to create a REST service in my django application using djangoRestframework django-rest.
I have model Student in MyModels.py which is has follows,
from django.db import models
class Student(object) :
def __init__(self,first_name,middle_name,last_name,subject_list=None):
self.first_name = first_name
self.last_name = last_name
self.middle_name = middle_name
self.subject_list = subject_list
class Subject(object) :
def __init__(self,name):
self.name = name
The below code is from views.py
def getStudentList(request):
my_list = []
my_list.append(Student(first_name="Alajandro",middle_name="Santana",last_name="Gonzales"))
my_list.append(Student(first_name="Timothy",middle_name="Carlos",last_name="Robins"))
my_list.append(Student(first_name="Nathan",middle_name="Nick",last_name="Mathews"))
my_list.append(Student(first_name="John",middle_name="Nelson",last_name="Kennedy"))
my_list.append(Student(first_name="Tom",middle_name="Kenny",last_name="Cruise"))
my_list.append(Student(first_name="Jerry",middle_name="Sanvile",last_name="McGuire"))
my_list.append(Student(first_name="Jason",middle_name="San",last_name="Statham"))
student_list = []
for student in my_list:
student_ser = rest.StudentSerialize(student)
student_list.append(student_ser.data.copy())
json = JSONRenderer().render(student_list)
return HttpResponse(json)
The url mapping in urls.py is
url(r'^studentList/', views.getStudentList),
The response is get from URL http://127.0.0.1:8083/studentList/
is
[{"first_name":"Alajandro","middle_name":"Santana","last_name":"Gonzales"},{"first_name":"Timothy","middle_name":"Carlos","last_name":"Robins"},{"first_name":"Nathan","middle_name":"Nick","last_name":"Mathews"},{"first_name":"John","middle_name":"Nelson","last_name":"Kennedy"},{"first_name":"Tom","middle_name":"Kenny","last_name":"Cruise"},{"first_name":"Jerry","middle_name":"Sanvile","last_name":"McGuire"},{"first_name":"Jason","middle_name":"San","last_name":"Statham"}]
Now If I am interested in also passing subjects in the json. I am trying to do is one-to-many relationship between student and subjects. i.e one student can have many subjects.
So i am expecting this json as an output
[{
"name" : "Tom",
"subject_list" : [{..},{},{},....]
},{},{}...]
For this above I have done following changes in code
my MyModel.py file
from django.db import models
class Student(object) :
def __init__(self,first_name,middle_name,last_name,subject_list=None):
self.first_name = first_name
self.last_name = last_name
self.middle_name = middle_name
if subject_list is None:
self.subject_list = subject_list
class Subject(object) :
def __init__(self,name):
self.name = name
My MyREST.py contains
from MyModels import Student
from MyModels import Subject
from rest_framework import serializers
class StudentSerialize(serializers.Serializer):
first_name = serializers.CharField(max_length=200)
middle_name = serializers.CharField(max_length=200)
last_name = serializers.CharField(max_length=200)
My views.py contains
def get_student_list(request):
my_list = []
my_list.append(Student(first_name="Alajandro",middle_name="Santana",last_name="Gonzales"))
my_list.append(Student(first_name="Timothy",middle_name="Carlos",last_name="Robins"))
my_list.append(Student(first_name="Nathan",middle_name="Nick",last_name="Mathews"))
my_list.append(Student(first_name="John",middle_name="Nelson",last_name="Kennedy"))
subject_list = []
subject_list.append(Subject(name="Computer Science"))
subject_list.append(Subject(name="Physics"))
subject_list.append(Subject(name="Chemistry"))
my_list.append(Student(first_name="Jason",middle_name="San",last_name="Statham",subject_list=subject_list))
my_list.append(Student(first_name="Tom",middle_name="Kenny",last_name="Cruise",subject_list=subject_list))
my_list.append(Student(first_name="Jerry",middle_name="Sanvile",last_name="McGuire",subject_list=subject_list))
student_list = []
for student in my_list:
student_ser = rest.StudentSerialize(student)
student_list.append(student_ser.data.copy())
json = JSONRenderer().render(student_list)
return HttpResponse(json)
And in urls.py I added
url(r'^student_list/', views.get_student_list),
Now If I hit the URL I still get json output as
[{"first_name":"Alajandro","middle_name":"Santana","last_name":"Gonzales"},{"first_name":"Timothy","middle_name":"Carlos","last_name":"Robins"},{"first_name":"Nathan","middle_name":"Nick","last_name":"Mathews"},{"first_name":"John","middle_name":"Nelson","last_name":"Kennedy"},{"first_name":"Jason","middle_name":"San","last_name":"Statham"},{"first_name":"Tom","middle_name":"Kenny","last_name":"Cruise"},{"first_name":"Jerry","middle_name":"Sanvile","last_name":"McGuire"}]
But I don't get the subjects.
What I should add in class SudentSerialize so that if I set subject_list in object of student, I will get json of Student an the property subject_list will have subject json array.
Upvotes: 0
Views: 1127
Reputation: 10228
You have to use nested Relationships to do this job :
Here is an example :
class SubjectSerializer(serializers.Serializer):
name = serializers.CharField(max_length=200)
class Meta:
fields = [
'name',
]
class StudentSerializer(serializers.Serializer):
first_name = serializers.CharField(max_length=200)
middle_name = serializers.CharField(max_length=200)
last_name = serializers.CharField(max_length=200)
# nested relationship
subjects = SubjectSerializer(many=True)
class Meta:
fields = [
'first_name',
'last_name',
'middle_name',
'subjects'
]
Read Django Rest Framework - Nested relationships for more information
Upvotes: 1
Reputation: 20966
You are looking for nested serializers. The documentation should cover everything you need to know about this.
Please also take some time to run the 6 steps of the tutorial. It will give you hints about how to use DRF.
Upvotes: 0