django rest框架 - 嵌套时如何表示反向关系?

时间:2015-11-04 17:49:10

标签: python django django-rest-framework

我正试图在巢穴上建立反向关系。

这是我的models.py(相关型号)

class Tblusergroups(models.Model):
    groupid = models.AutoField(db_column='groupId', primary_key=True) # Field name made lowercase.
    shortname = models.CharField(db_column='shortName', max_length=255) # Field name made lowercase.
    groupname = models.CharField(db_column='groupName', max_length=255, blank=True) # Field name made lowercase.
    departmentname = models.CharField(db_column='departmentName', max_length=255, blank=True) # Field name made lowercase.
    subdepartmentname = models.CharField(db_column='subDepartmentName', max_length=255, blank=True) # Field name made lowercase.
    subdepartmentshortname = models.CharField(db_column='subDepartmentShortName', max_length=255, blank=True) # Field name made lowercase.
    divisionname = models.CharField(db_column='divisionName', max_length=255, blank=True) # Field name made lowercase.
    divisionshortname = models.CharField(db_column='divisionShortName', max_length=255, blank=True) # Field name made lowercase.
    subdivisionname = models.CharField(db_column='subDivisionName', max_length=255, blank=True) # Field name made lowercase.
    seniormanager = models.CharField(db_column='seniorManager', max_length=255, blank=True) # Field name made lowercase.
    manager = models.CharField(max_length=255, blank=True)
    supportcontactemailaddress = models.CharField(db_column='supportContactEmailAddress', max_length=255, blank=True) # Field name made lowercase.
    comments = models.TextField(blank=True)
    isactive = models.NullBooleanField(db_column='isActive') # Field name made lowercase.
    recordownerid = models.IntegerField(db_column='recordOwnerId', blank=True, null=True) # Field name made lowercase.
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True) # Field name made lowercase.
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True) # Field name made lowercase.
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.
    class Meta:
        managed = False
        db_table = 'tblUserGroups'

class Tblbmcgroups(models.Model):
    bmcid = models.AutoField(db_column='bmcId', primary_key=True)
    groupid = models.ForeignKey(Tblusergroups, db_column='groupId', related_name='bmc_code', to_field='groupid')
    bmccode = models.CharField(db_column='bmcCode', max_length=255)

    class Meta:
        managed = False
        db_table = 'tblBmcGroups'

class Tblservers(models.Model):
    serverid = models.IntegerField(db_column='serverId', primary_key=True) # Field name made lowercase.
    servername = models.CharField(db_column='serverName', max_length=255) # Field name made lowercase.
    environmentid = models.ForeignKey(Tbldomaincodes, db_column='environmentId', related_name='+', to_field='id') # Field name made lowercase.
    isvirtual = models.NullBooleanField(db_column='isVirtual') # Field name made lowercase.
    locationid = models.ForeignKey(Tbldomaincodes, db_column='locationId', related_name='+', to_field='id') # Field name made lowercase.
    managedbyid = models.ForeignKey(Tbldomaincodes, db_column='managedById', related_name='+', to_field='id') # Field name made lowercase.
    operatingsystem = models.CharField(db_column='operatingSystem', max_length=255, blank=True) # Field name made lowercase.
    model = models.CharField(max_length=255, blank=True)
    assettag = models.CharField(db_column='assetTag', max_length=255, blank=True) # Field name made lowercase.
    inceptiondate = models.DateTimeField(db_column='inceptionDate', blank=True, null=True) # Field name made lowercase.
    comments = models.TextField(blank=True)
    assetstatusid = models.ForeignKey(Tbldomaincodes, db_column='assetStatusId', related_name='+', to_field='id') # Field name made lowercase.
    recordownerid = models.ForeignKey(Tblusergroups, db_column='recordOwnerId') # Field name made lowercase.
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True) # Field name made lowercase.
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True) # Field name made lowercase.
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.
    class Meta:
        managed = False
        db_table = 'tblServers'

我正在做一个非常简单的serializers.py

class TblusergroupsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tblusergroups
        fields = ('groupid', 'shortname', 'groupname', 'departmentname', 'subdepartmentname', 'subdepartmentshortname',
                  'divisionname', 'divisionshortname', 'seniormanager', 'manager', 'supportcontactemailaddress',
                  'comments', 'isactive', 'recordownerid', 'lastmodifieddate', 'lastmodifiedby', 'bmccode')

class TblserversSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tblservers
        fields = ('serverid', 'servername', 'isvirtual', 'locationid', 'managedbyid', 'operatingsystem', 'model',
                  'assettag', 'inceptiondate', 'comments', 'assetstatusid', 'recordownerid', 'lastmodifieddate',
                  'lastmodifiedby')
        depth = 2

class TblapplicationsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tblapplications
        depth = 1

如果我只使用TblusergroupsSerializer我可以在bmccode上看到相反的关系问题是当我尝试在使用TblserversSerializer

时表示问题

我猜测序列化器可能更容易拥有def get_bmccode

1 个答案:

答案 0 :(得分:0)

您应该显式设置嵌套的序列化程序。

例如(取自http://www.django-rest-framework.org/api-guide/relations/#nested-relationships):

class TrackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Track
        fields = ('order', 'title', 'duration')

class AlbumSerializer(serializers.ModelSerializer):
    tracks = TrackSerializer(many=True, read_only=True)

    class Meta:
        model = Album
        fields = ('album_name', 'artist', 'tracks')