过滤反向关系 [英] Filtering on reverse relations

查看:151
本文介绍了过滤反向关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有tastypie和Django设置,他们工作得很好,我可以过滤,并通过HTTP修补对象。



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



所以我的Django模型就像这样,每个库对象都有一个Multiplex索引,每个Multiplex索引可能有多个库被使用:

  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 =
def __unicode __(self):
return%s(%s)%(self.multiplex_index_name,self.type.name)
class Meta:
ordering = ['multiplex_index_name']


类库(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资源是这样的(我已经尝试过各种组合):
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
class Meta:
queryset = Library.objects.all()。order_by(' - date_prepared')
resource_name ='library'
paginator_class = Paginator
serializer = PrettyJSONSerializer()
filters = {
'sample':ALL_WITH_RELATIONS,
'multiplexindex':ALL_WITH_RELATIONS,
'loadedwith' ALL_WITH_RELATIONS,
'id':ALL,
'name':ALL
}


class MultiplexIndexResource(ModelResource):
library = fields.ToManyField(' sort.api.LibraryResource',attribute ='library_set',related_name ='library')
class Meta:
queryset = MultiplexIndex.objects.all()
resource_name ='multiplexindex'
serializer = PrettyJSONSerializer()
filters = {
'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 queryset API中做相当的这个。

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

所以我的网址如下:

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

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



我尝试将其更改为库,但是我得到的错误是:
'MultiplexIndex'对象没有属性'library')



似乎属性=我的MultiplexIndexResource的'library_set'导致问题。当它设置为库集时,它返回一个相关的管理器,但是过滤器被设置为library_set__name = 515D。当它设置为库时,在MultiplexIndex表上没有字段可以过滤。



所以有一个简单的方法来设置过滤,工作在相反的方向?我是否缺少某些东西?

解决方案

MultipleIndexResource library_set 作为kwargs,但作为参考。所以,这样替换 attribute ='library_set',只需 library_set

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

如果需要,添加 full = True


I have tastypie and Django set up, and they work well, I can filter, and patch objects via HTTP.

Now I want to try filtering my results on a reverse relationship and I am having trouble getting it working.

So my Django models are like so, each library object has a multiplex index, and each multiplex index may have multiple libraries it is used with :

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

my Tastypie resources are like so (I have tried various combinations):

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

I can filter in the "forward direction" properly. The following will return all libraries with the multiplex index 92hp.

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

However, when I try to do a filter on the reverse relation, I always get errors. I want to do the equivalent of this in the Django queryset API.

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

so my URL is as follows:

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

In this case I get the error: Cannot resolve keyword 'library_set' into field.

(I tried changing this to library, but then the error I get is : 'MultiplexIndex' object has no attribute 'library')

It seems that the attribute='library_set' of my MultiplexIndexResource is causing problems. When it is set to library set, it is returning a related manager, but then the filter is being set to "library_set__name=515D". When it is set to library, then there is no field on the MultiplexIndex table for it to filter on.

So is there a simple way to set up filtering so it will work in the reverse direction? Am i missing something?

解决方案

On MultipleIndexResource, don't treat library_set as kwargs, but as args. So, replace the attribute = 'library_set' with just library_set like this :

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

Add full = True if you want.

这篇关于过滤反向关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆