【问题标题】:how to change serialized JSON structure django rest framwork如何更改序列化的 JSON 结构 django rest 框架
【发布时间】:2016-11-03 16:08:27
【问题描述】:

我想知道是否可以更改我的 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"
         },
    }

更新: 在 cmets 中为请求添加序列化程序和模型

对象序列化器:

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)

【问题讨论】:

  • 请添加您的序列化器和对应的型号。
  • 完成。让我知道是否需要其他代码部分

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


【解决方案1】:

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

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
            }
        }

【讨论】:

  • 这与我正在做的类似,但模型有许多外键关系,我认为这是造成这种影响的原因。我已使用我的模型和此模型的序列化程序更新了票证。
  • 看起来不像我需要的:(
  • 感谢您的调查
【解决方案2】:

您可以在ParaSerializer 上添加带有source 参数的disciplineroom 字段。

这些字段将从提到的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'
        )

【讨论】:

  • 感谢您的调查
猜你喜欢
  • 2015-10-29
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 2015-06-29
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多