python - Using django-filter, why do unspecified or invalid lookup types return all results? -
here's simple django rest framework/django-filter code example:
class mymodelfilter(django_filters.filterset): class meta: model = mymodel fields = {'my_str_field': ['exact']} class mymodellist(generics.listapiview): queryset = mymodel.objects.all() filter_class = mymodelfilter def get(self, request, format=none): items = self.filter_queryset(self.queryset) # apply filters serializer = mymodelserializer(items, many=true) return response(serializer.data) when make api call, exact lookup type works expected, returning matched objects:
/myobjects/?my_str_field=somevalue if use icontains, see did not specify 1 of supported lookup types, all objects returned, if filter wasn't applied:
/myobjects/?my_str_field__icontains=this_can_be_anything furthermore, can use invalid lookup type , there no error, with, again, all objects returned:
/myobjects/?my_str_field__this_can_be_anything=this_can_be_anything this can misleading because front-end developer doesn't have access back-end code can happily think fine , use returned objects. expect, if not error, @ least empty result set latter 2 cases. doing wrong?
update: appears should using strictness setting so:
from django_filters.filterset import strictness class mymodelfilter(django_filters.filterset): # throw exception on errors instead of returning empty results strict = strictness.raise_validation_error class meta: model = mymodel fields = {'my_str_field': ['exact']} unfortunately, still doesn't result in error, original question still stands.
if server doesn't recognize query string parameter, typical behavior ignore parameter. there's no standard or rfc specifies how handle unexpected query string parameters. many websites , web frameworks liberally accept requests, , not perform validation reject requests contain superfluous or misspelled query parameters.
in other words, not specific django rest framework.
this feature makes ajax cache busting possible. jquery add parameter called _ random value every single ajax request make sure request has unique url , not cached anywhere. not work if server returned error on receiving unexpected query parameter.
Comments
Post a Comment