Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango admin filter list with custom model manager extra methods
    text
    copied!<p>I have an <code>Address</code> model that contains two float fields, <code>lat</code> and <code>lng</code>. I have written a custom model manager with a <code>nearby(lat, lng, distance)</code> method that uses raw SQL to return only <code>Address</code>es which lie within a certain radius. (GeoDjango appeared to be overkill).</p> <p>Example call: <code>Address.objects.nearby(53.3, 13.4, 10)</code> (returns QuerySet)</p> <p>Now I want to dynamically filter <code>Address</code> objects in the Django admin using this method (ideally letting the user pick a location on a Google map and a max distance using a slider). I have no idea how to achieve that. Can anyone point me in the right direction?</p> <p><strong>CLARIFICATION</strong> You don't need to write any JavaScript for me, I just want to know how to make Django admin evaluate extra Query parameters in the URL, such that I can do queries like <code>/admin/appname/address/?lat=53&amp;long=13&amp;dist=10</code>. I can then probably figure out how to stuff a Google map and the required JavaScript magic into the template myself.</p> <p><strong>UPDATE</strong> I've tried to overwrite <code>queryset</code> in the ModelAdmin like so:</p> <pre><code>def queryset(self, request): try: lat = float(request.REQUEST['lat']) lng = float(request.REQUEST['lng']) dist = int(request.REQUEST['dist']) matches = Address.objects.nearby(lat=lat, lng=lng, dist=dist) return matches except: return super(ReportAdmin, self).queryset(request) </code></pre> <p>However, the admin does not like it and returns with <code>?e=1</code>, without filtering the results.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload