wobbily_col
wobbily_col

Reputation: 11941

tastypie filtering

I am starting to use Django and tastypie, to interact with my database.

So I have a database with a number of tables, and I can set up filtering, so that I can use a URL like so, using chains of related Django objects to filter my results.

api/seq/mapping/?loadedwith__lane__flowcell__name=C16P5ACXX&loadedwith__lane__lane=8

the mapping resource is set up as follows:

class MappingResource(ModelResource):
    loadedwith = fields.ToOneField('sequencing.api.LoadedWithResource' , 'loadedwith' ) 
    class Meta:
        queryset = Mapping.objects.all()
        resource_name = 'mapping'
        allowed_methods = ['get' , 'post' , 'put' , 'patch' ,  'delete']
        authorization = Authorization()                        
        serializer = PrettyJSONSerializer()
        filtering = {
            'loadedwith': ALL_WITH_RELATIONS,
            'reference_filename' : ALL
        }

This all works fine, as I would expect it to.

Now I noticed a problem when I messed up the URL. If I miss out one of the loadedwith objects on the chain e.g.

loadedwith__lane__lane=8

and instead use

lane__lane=8

so the url ends up as:

api/seq/mapping/?loadedwith__lane__flowcell__name=C16P5ACXX&loadedwith__lane__lane=8

Now this returns the results filtered by the first part:

loadedwith__lane__flowcell__name=C16P5ACXX

but basically ignoring the second part

lane__lane=8

I would have expected it to throw an error, or not return anything. Is there a way to configure tastypie to throw an error in this situation? (Or is this an expected behaviour for some reason I am unaware of)?

Upvotes: 1

Views: 3393

Answers (1)

mpaolini
mpaolini

Reputation: 1344

default tastypie behaviour is to ignore filters that don't match fields [1].

The only way to alter this behaviour is to override ModelResource build_filters() method.

[1] https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py#L1784

Upvotes: 3

Related Questions