DRF - 跨2个型号过滤

时间:2015-11-21 05:17:50

标签: python django django-rest-framework django-filter

我正在使用遗留数据库,我在表A上设置了序列化程序,就像这样 -

class TblapplicationsSerializer(serializers.ModelSerializer):

    class Meta:
        model = Tblapplications
        fields = ('applicationid', 'applicationname', 'description', 'drtierid', 'saglink', 'supportinstructions',
                  'defaultincidentpriorityid', 'applicationorigintypeid', 'installationtypeid', 'comments',
                  'lastmodifieddate', 'lastmodifiedby', 'assetstatusid', 'recordownerid', 'adl_app')
        depth = 2

我正在使用标准过滤器 -

class TblapplicationsFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name="applicationname", lookup_type="exact")
    env = django_filters.CharFilter(name="adl_app__environmentid__domain")

    class Meta:
        model = Tblapplications
        fields = ['applicationname', 'name', 'env']

这是横向走向的地方。我想要做的是过滤我的网址/api/applications/?name=xxx&env=DEV。然后它将返回应用程序以及与DEV环境链接的任何数据库。这个名字很容易理解,但唯一的一点是我发现环境是让应用程序的api点触及连接两者的中间表,但是它返回多个值,因为每次使用单独的数据库引用应用程序时都会抓取它。 / p>

我根据给出的注释和序列化程序更新了Serializer和Filter,而&env=DEV没有返回所有相应的数据(domain以反向关系嵌套)。然后我希望我的过滤器根据它过滤结果。这意味着它需要知道将反向关系的结果限制为仅从嵌套值提供的结果。

如果你看到我的模特 -

class Tblapplicationdatabaselinks(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)
    applicationid = models.ForeignKey('Tblapplications', db_column='applicationId', to_field='applicationid',
                                      related_name='adl_app')
    dbid = models.ForeignKey('Tbldatabases', db_column='dbId', to_field='id', related_name='adl_db')
    environmentid = models.ForeignKey('Tbldomaincodes', db_column='environmentId', to_field='id',
                                      related_name='adl_envlink')
    comments = models.TextField(blank=True)
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.

    class Meta:
        managed = False
        db_table = 'tblApplicationDatabaseLinks'

class Tblapplications(models.Model):
    applicationid = models.AutoField(db_column='applicationId', primary_key=True)
    applicationname = models.CharField(db_column='applicationName', max_length=255)
    description = models.TextField(blank=True)
    drtierid = models.ForeignKey(Tbldomaincodes, db_column='drTierID', blank=True, null=True, to_field='id',
                                 related_name='app_drtier')
    saglink = models.TextField(db_column='sagLink', blank=True)
    supportinstructions = models.TextField(db_column='supportInstructions', blank=True)
    defaultincidentpriorityid = models.IntegerField(db_column='defaultIncidentPriorityId', blank=True, null=True)
    applicationorigintypeid = models.IntegerField(db_column='applicationOriginTypeId')
    installationtypeid = models.ForeignKey(Tbldomaincodes, db_column='installationTypeId', to_field='id',
                                           related_name='app_insttype')
    comments = models.TextField(blank=True)
    assetstatusid = models.ForeignKey(Tbldomaincodes, db_column='assetStatusId', to_field='id',
                                      related_name='app_status')
    recordownerid = models.ForeignKey(Tblusergroups, db_column='recordOwnerId', blank=True, null=True,
                                      to_field='groupid', related_name='app_owner')
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.

    class Meta:
        managed = False
        db_table = 'tblApplications'

class Tbldatabases(models.Model):
    dbid = models.AutoField(db_column='dbId', primary_key=True)
    dbname = models.CharField(db_column='dbName', max_length=255)
    serverid = models.ForeignKey('Tblservers', db_column='serverId', to_field='serverid', related_name='db_serv')
    servicename = models.CharField(db_column='serviceName', max_length=255, blank=True)
    dbtypeid = models.IntegerField(db_column='dbTypeId', blank=True, null=True)
    inceptiondate = models.DateTimeField(db_column='inceptionDate', blank=True, null=True)
    comments = models.TextField(blank=True)
    assetstatusid = models.IntegerField(db_column='assetStatusId')
    recordownerid = models.IntegerField(db_column='recordOwnerId', blank=True, null=True)
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.

    class Meta:
        managed = False
        db_table = 'tblDatabases'

class Tbldomaincodes(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True)
    domain = models.CharField(primary_key=True, max_length=255)
    displayname = models.CharField(db_column='displayName', primary_key=True, max_length=255)
    displayorder = models.IntegerField(db_column='displayOrder', blank=True, null=True)
    comments = models.TextField(blank=True)
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.

    class Meta:
        managed = False
        db_table = 'tblDomainCodes'

1 个答案:

答案 0 :(得分:1)

扩展您的过滤器集并引用其他模型中的字段:

class TblapplicationsFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name="applicationname", lookup_type="exact")
    env = django_filters.CharFilter(name="environmentid__name")
    #                                    ^^^^^^^^^^^^^^^^^^^

    class Meta:
        model = Tblapplications
        fields = ['applicationname', 'name', 'env']

此外,您可能希望将ForeignKey字段命名为不带id后缀,即Django约定。在Django中,当您访问Tblapplications.environmentid时,它通常是一个模型实例,而不是实际的id整数本身。