Filtering on reverse relations

淺唱寂寞╮ 提交于 2019-12-11 01:38:30

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13382265/filtering-on-reverse-relations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!