如何更改序列化的JSON结构django rest framwork

时间:2016-07-01 09:43:15

标签: python json django serialization django-rest-framework

我想知道是否有可能改变我的JSON结构。 当前它看起来像这样:

{
    "para_subject": {
      "discipline": "MATAN"
    },
    "para_room": {
      "room": "210"
    },
    "para_professor": {
        "user": {
            "username": "yyyy",
            "email": "yyyy.yyyy@gmail.com",
            "first_name": "yyyy",
            "last_name": "yyy"
         },
        "middle_name": "xxxxxx"
     },

}

将此更改为最佳方法是什么:

  {
    "discipline": "MATAN",
    "room": "210",
    "para_professor": {
        "username": "yyyy",
         "email": "yyyy.yyyy@gmail.com",
         "first_name": "yyyy",
         "last_name": "yyy"
         "middle_name": "xxxx"
         },
    }

更新: 在注释中的请求中添加序列化器和模型

对象序列化器:

class ParaSerializer(serializers.ModelSerializer):
    para_subject = DisciplineSerializer()
    para_room = RoomSerializer()
    para_professor = ProfessorProfileForScheduleSerializer(read_only=True)
    para_number = ParaTimeSerializer()
    para_day = WorkingDaySerializer()
    # para_group = StudentGroupSerializer()

    class Meta:
        model = Para
        fields = (
            'para_subject',
            'para_room',
            'para_professor',
            'para_number',
            'para_day',
            'para_group',
            'week_type'
        )

对象模型:

class Para(models.Model):

    class Meta(object):
        verbose_name = u"Class"
        verbose_name_plural = u"Classes"

    para_subject = models.ForeignKey(
        'Disciplines',
        blank=True,
        null=True,
        verbose_name=u"Discipline"
    )
    para_room = models.ForeignKey(
        'Rooms',
        blank=True,
        null=True,
        verbose_name=u"Room"
    )
    para_professor = models.ForeignKey(
        'students.ProfileModel',
        blank=True,
        null=True,
        verbose_name=u"Professor"
    )
    para_number = models.ForeignKey(
        'ParaTime',
        blank=True,
        null=True,
        verbose_name=u"Class Starts/Ends"
    )
    para_day = models.ForeignKey(
        WorkingDay,
        blank=True,
        null=True,
        verbose_name=u"Working day")

    para_group = models.ForeignKey(
        'StudentGroupModel',
        blank=True,
        null=True,
        verbose_name=u"Student Group"
    )
    week_type = models.BooleanField(
        default=True,
        verbose_name=u"Is week even"
    )

    def __unicode__(self):
        return u"%s %s" % (self.para_subject, self.para_room)

2 个答案:

答案 0 :(得分:2)

这取决于您使用的序列化程序/模型,但通常可以使用如下所示的序列化程序:

class Serializer1(serializers.Serializer):
    discipline = serializers.CharField()
    room = serializers.IntegerField()
    para_professer = Serializer2()

class Serializer2(serializers.Serializer):
    username = serializers.CharField()
    email = serializers.CharField()
    first_name = serializers.CharField()
    last_name = serializers.CharField()
    middle_name = serializers.CharField()

在这里,您可以找到django rest框架的嵌套序列化器文档 http://www.django-rest-framework.org/api-guide/relations/#nested-relationships

根据您问题中的新信息,您可以覆盖序列化程序的.to_representation()方法:

class ParaSerializer(serializers.ModelSerializer):

    class Meta:
        model = Para
        fields = (
            'para_subject',
            'para_room',
            'para_professor',
            'para_number',
            'para_day',
            'para_group',
            'week_type'
        )

    def to_representation(self, instance):
        return {
            'discipline': instance.para_subject.name,
            'room': instance.para_room.number,
            'para_professor': {
                'username': instance.para_professor.username,
                'email': instance.para_professor.email,
                'first_name': instance.para_professor.first_name,
                'last_name': instance.para_professor.last_name,
                'middle_name': instance.para_professor.middle_name
            }
        }

答案 1 :(得分:1)

您可以在discipline上添加roomsourceParaSerializer个参数字段。

这些字段将从提到的source中获取值,并将包含在输出中。

class ParaSerializer(serializers.ModelSerializer)
    # define 'discipline' and 'room' fields
    discipline = serializers.CharField(source='para_subject.discipline', read_only=True)
    room = serializers.CharField(source='para_room.room', read_only=True)

    class Meta:
        model = Para
        fields = (
            'discipline', # include this field
            'room', # include this field
            'para_professor',
            'para_number',
            'para_day',
            'para_group',
            'week_type'
        )