gpt4 book ai didi

python - 如何更改序列化的JSON结构djangorest框架

转载 作者:行者123 更新时间:2023-11-30 22:53:37 33 4
gpt4 key购买 nike

我想知道是否可以更改我正在发送的 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)

最佳答案

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

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

关于python - 如何更改序列化的JSON结构djangorest框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38142137/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com