2016-07-01 41 views
1

我想知道是否有可能改变我的JSON的结构即时sedinding。 currenyly它看起来像这样:如何更改序列化的JSON结构django休息framwork

{ 
    "para_subject": { 
     "discipline": "MATAN" 
    }, 
    "para_room": { 
     "room": "210" 
    }, 
    "para_professor": { 
     "user": { 
      "username": "yyyy", 
      "email": "[email protected]", 
      "first_name": "yyyy", 
      "last_name": "yyy" 
     }, 
     "middle_name": "xxxxxx" 
    }, 

} 

是什么将其更改为这一目标的最佳方式:

{ 
    "discipline": "MATAN", 
    "room": "210", 
    "para_professor": { 
     "username": "yyyy", 
     "email": "[email protected]", 
     "first_name": "yyyy", 
     "last_name": "yyy" 
     "middle_name": "xxxx" 
     }, 
    } 

UPDATE: 在添加串行器与模型在注释请求

对象串行器:

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

对象M奥德尔:

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) 
+0

请添加您的序列化器和相应的型号。 –

+0

完成。让我知道如果代码的其他部分需要 –

回答

1

您可以在ParaSerializer上添加disciplineroom字段,source参数。

这些字段将从提及的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' 
     ) 
+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框架 的嵌套串行DOC 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 
      } 
     } 
+0

是类似于我在做什么,但模型有很多外键关系,我认为这是造成这种影响的原因。我已经使用我的模型和序列化程序更新了此模型的票证。 –

+0

看看SlugRelatedField:http://www.django-rest-framework.org/api-guide/relations/#slugrelatedfield –

+0

看起来不像我需要的东西:( –