1

I have a django model with two classes Students and Courses. In a get request method I wish to extract the student information and the course s/he is taking (they can take only one course). Since a student can be registered and not have an active course, my return result should either include only the student data or the student data as well as the name of the course s/he is taking.

the naive version for this kind of code will be:

student = Students.objects.filter(id=student_id)
if student[0].activeCourse:
    studentCourse = UniversityCourses.objects.filter(id=student[0].activeCourse)
    combined_data = list(chain(student, studentCourse))
    output = serializers.serialize('json', combined_data, fields=('name', 'age' 'id', 'courseName'))
else:
    output = serializers.serialize('json', user, fields=('name', 'age' 'id'))

 return HttpResponse(output, content_type="application/json")

Question 1: if the Courses table has also a field called name and not courseName when calling serialize how to distinguish between student.name and studentCourse.name?

Question 2: Can it be done without redundant code? that means my code will look something like this:

student = Students.objects.filter(id=student_id)
output = serializers.serialize('json', user, fields=('name', 'age' 'id'))
if student[0].activeClass:
    #add the courseName to the already defined output

 return HttpResponse(output, content_type="application/json")
6
  • I don't know if it was just me but I didn't understand the question ? Commented May 3, 2015 at 19:37
  • ok, after 15 minutes, I still have no idea what is your problem. (Too many ambiguity in your sentences). "your output looks like:...???" "add that class info to the output???" What.... I don't get it... Classes..objects #here I'd like to add to output the field className Commented May 3, 2015 at 19:56
  • @RafaelCardoso can you please let me know where to clarify, I'm not an english speaker Commented May 3, 2015 at 21:51
  • @Yeo please let me know if now the question and my problem are clearer. Commented May 3, 2015 at 22:09
  • You can distinguish between both outputs by checking if 'courseName' is in the data you passed. If it is, then you know output variable is the one you set inside the if statement; if it isn't, you know it was set in the else statement. Commented May 3, 2015 at 22:28

1 Answer 1

0

A sane simple solution is to override the get_serialiser_class method like that:

def get_serializer_class(self):
    if self.get_object().activeCourse:
        return ActiveProfileSerializer
    else:
        return ProfileSerializer

the ActiveProfileSerializer has only the fields provided for a Student with active courses or whatever you want to provide extra. Those fields should be defined in the serialiser as a nested relationship:

Then on your views you only need to call:

serialiser = self.get_serializer_class()

to get the right serialiser data.

The good thing about this approach is that it is more decoupled to return the correct serialiser class for your views.

Sign up to request clarification or add additional context in comments.

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.