找到你要的答案

Q:Django serialize - adding another parameter to the serialized output

Q:Django序列化添加另一个参数序列化输出

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")

我有两个班的学生,课程有一个Django模型。在一个GET请求方法我希望提取学生信息和课程/他正在采取(他们只能采取一门课程)。由于学生可以注册,而不是一个活跃的过程中,我的返回结果应该只包括学生数据或学生数据,以及该课程的名称/他正在采取。

这种代码的幼稚版本将是:

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")

问题1:如果课程表也一场所谓的名称而不是调用序列化courseName时如何区分student.name和studentcourse.name之间?

问题2:没有冗余代码可以做到吗?这意味着我的代码看起来像这样:

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")
answer1: 回答1:

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.

一个理智的简单的解决办法是覆盖get_serialiser_class方法一样:

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

的activeprofileserializer只有领域提供学生活动课程或任何你想提供更多的。这些领域应在serialiser定义为嵌套关系:

然后在你的意见,你只需要打电话:

serialiser = self.get_serializer_class()

为了得到正确的serialiser数据。

这种方法的好处是,它更是解耦返回正确的对你的看法serialiser类。

python  django  concatenation