5

I have a model that uses latitude and longitude fields for location. One of the queries that I want to run using query params is search around a specific radius. I have the queryset read and I override it:

queryset = super().get_queryset(request)
if 'radius' in request.GET:
    queryset = queryset.in_distance(request.GET['radius'], fields=['location__latitude',location__longitude'], points=[lat, lon])
return queryset

When calling my admin page with /admin/dal/listing?radius=50 I get redirected to the admin without the query string.

Followed Django's code and found this:

# At this point, all the parameters used by the various ListFilters
# have been removed from lookup_params, which now only contains other
# parameters passed via the query string. We now loop through the
# remaining parameters both to ensure that all the parameters are valid
# fields and to determine if at least one of them needs distinct(). If
# the lookup parameters aren't real fields, then bail out.
try:
    for key, value in lookup_params.items():
        lookup_params[key] = prepare_lookup_value(key, value)
        use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, key)
    return filter_specs, bool(filter_specs), lookup_params, use_distinct
except FieldDoesNotExist as e:
    six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2])

In a nutshell because the query string is not a known field django gracefully panics and redirect a not filter admin page.

What can I do ?

1 Answer 1

8

You should pop custome parameters from request. Try this:

def get_queryset(self, request):
    queryset = super().get_queryset(request)
    request.GET = request.GET.copy()
    radius = request.GET.pop('radius', None)
    if radius:
        queryset = queryset.in_distance(
            radius[0], fields=['location__latitude', 'location__longitude'],
            points=[lat, lon])
    return queryset
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer, but look closely at what I wrote, django removes any parameters that are not part of the model. 'radius' is not part of the model
I have solved the same problem just now. I tried to pass query params, which aren't part of the model (number), and had redirect to /applabel/users/?e=1.I added code, which i had wrote above and it's work. I refresh the page url: /applabel/users/?number=5, and get the right result (filtering by number). (Django1.11)
Once I POP the custom param, while the admin page loads fine and a more custom list (based on the query param is seen), when I click to order a column or filter via a admin filter, the query param is not persisted (and its effects are lost). How can we solve that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.