OK cool I’ll share some code and elaborate on what I supplied in the SO question. My (@pauloxnet 's) viewsets.py:
class MarkerViewSet(viewsets.ReadOnlyModelViewSet):
"""Marker view set."""
bbox_filter_field = "geom"
filter_backends = (filters.InBBoxFilter,)
queryset = Tablename.objects.all()
serializer_class = MarkerSerializer
pagination_class = GeoPaginator
This works nicely despite me not really understanding it. The response entries are what are found within the bbox supplied in the url we defined in map.js: "const markers_url = /api/markers/?in_bbox=${map.getBounds().toBBoxString()}
", which comes in like “/api/markers/?in_bbox=149.7503471374512,-36.96112442377135,150.1019096374512,-36.90486603587978”. Side note: this is far too many decimals but I’ll figure that out later. All works beautifully.
I’d like to expand on this functionality to also filter by other criteria if they’re found in GET values, but I don’t know how. I’ve already done this for another part of my project with views.py and can easily expand upon this by adding more qs = qs.filter()
:
def EgFilterView(request):
qs = Tablename.objects.all()
date_min_query = request.GET.get('dmin')
min_date = 1970
qs = qs.filter(date__gte=dt.datetime(int(date_min_query), 1, 1, tzinfo=pytz.UTC))
context = {
'queryset': qs,
'dmin': min_date,
}
return render(request, "main_page.html", context)
I’d like to expand on what viewsets.py is doing with similar ease but I don’t know how. So for example if the dmin
GET value contains an int corresponding to a year I’d like to filter results to be everything in the bbox and occurring after dmin
. I think I might be able to by adding to filter_backends = (filters.InBBoxFilter,)
, but I don’t know where to start.
My interpretation of viewsets.py documentation (ty for the link) is that viewsets.py accomplishes the same as views.py in a more advanced and elegant way but I’m not at that level yet. I’m thinking it might be best to try converting all of this to views.py but I don’t know if that’s even possible. Maybe I’m missing something?
Sorry for the silly questions. Thank you so much for your patience and help!