【问题标题】:Filtering on reverse relations过滤反向关系
【发布时间】:2012-11-03 03:39:19
【问题描述】:

我设置了tastepie 和Django,它们运行良好,我可以通过HTTP 过滤和修补对象。

现在我想尝试过滤反向关系的结果,但无法正常工作。

所以我的 Django 模型是这样的,每个库对象都有一个多路索引,每个多路索引可能有多个与之一起使用的库:

class MultiplexIndex(models.Model):
    multiplex_index_name = models.CharField(max_length = 100,   unique=True )
    multiplex_index_seq = models.CharField(max_length = 100,   null=True, blank=True) 
   def __unicode__(self):
        return  "%s (%s)" % ( self.multiplex_index_name , self.type.name)
    class Meta:
        ordering = ['multiplex_index_name']


class Library(models.Model):
    sample = models.ForeignKey(Sample, db_index=True)
    date_prepared = models.DateField(null=True, db_index=True )
    multiplex_index = models.ForeignKey(MultiplexIndex , null=True , blank=True)
    ...
    ...
    etc....

我的 Tastypie 资源是这样的(我尝试过各种组合):

class LibraryResource(ModelResource):
    sample = fields.ToOneField('sequencing.api.SampleResource', 'sample' )
    multiplexindex = fields.ToOneField('sequencing.api.MultiplexIndexResource' , 'multiplex_index'  , related_name='multiplexindex'  , null=True )  
    loadedwith_set = fields.ToManyField('sequencing.api.LoadedWithResource' ,    'loadedwith_set' , null=True)
    class Meta:
        queryset = Library.objects.all().order_by('-date_prepared')
        resource_name = 'library'
        paginator_class = Paginator
        serializer = PrettyJSONSerializer()
        filtering = {
            'sample': ALL_WITH_RELATIONS ,
            'multiplexindex' : ALL_WITH_RELATIONS , 
            'loadedwith' : ALL_WITH_RELATIONS , 
            'id' : ALL ,
            'name': ALL
        }


class MultiplexIndexResource(ModelResource):
    library = fields.ToManyField('sequencing.api.LibraryResource', attribute='library_set' ,    related_name='library' )
    class Meta:
        queryset = MultiplexIndex.objects.all()
        resource_name = 'multiplexindex'
        serializer = PrettyJSONSerializer()
        filtering = {
            'multiplex_index_name' : ALL ,
            'library' : ALL_WITH_RELATIONS , 
        }

我可以正确过滤“前进方向”。以下将返回多路索引为 92hp 的所有库。

http://127.0.0.1:8000/api/seq/library/?multiplexindex__multiplex_index_name=92hp&format=json

但是,当我尝试对反向关系进行过滤时,总是会出错。我想在 Django 查询集 API 中做同样的事情。

MultiplexIndex.objects.filter(library__name='515D')

所以我的网址如下:

http://127.0.0.1:8000/api/seq/multiplexindex/?library__name=515D&format=json

在这种情况下,我收到错误: 无法将关键字“library_set”解析为字段。

(我尝试将其更改为库,但随后我得到的错误是: “MultiplexIndex”对象没有“库”属性)

我的 MultiplexIndexResource 的 attribute='library_set' 似乎引起了问题。当它设置为库集时,它会返回一个相关的管理器,但是过滤器被设置为“library_set__name=515D”。当它设置为库时,MultiplexIndex 表上没有可供它过滤的字段。

那么有没有一种简单的方法来设置过滤,使其反向工作?我错过了什么吗?

【问题讨论】:

    标签: django filtering tastypie one-to-many


    【解决方案1】:

    MultipleIndexResource 上,不要将 library_set 视为 kwargs,而是视为 args。因此,将 attribute = 'library_set' 替换为 library_set,如下所示:

    library = fields.ToManyField('sequencing.api.LibraryResource', 'library_set' , related_name='library')
    

    如果需要,请添加 full = True

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-23
      • 2021-11-02
      • 2019-11-06
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      相关资源
      最近更新 更多