Dear @KenWhitesell
(I wrote this post at the same time of my tries. You will read, it works at the end)
Thanks so much to follow me and your patience, but I still do not understand.
I do not understand where is should be from map.viewsets import MarkerViewSet
I removed all my tries and I try again your suggestion.
So, we are agree this is correct
async function load_markers() {
const markers_url = `/api/map/1/`; // this is correct
console.log("markers_url: ",markers_url);
const response = await fetch(markers_url);
//console.log("response: ",response);
const geojson = await response.json();
console.log("geojson: ",geojson);
return geojson;
}
Then the project/urls.py, I follow your example:
path("api/map/<int:idfield>/", MarkerViewSet.as_view(), name="marker_view_set")
But I beleive, in any case, the api.py file is not correct
router = routers.DefaultRouter()
#router.register(r"map", MarkerViewSet) #!!!?????
#router.register(r"^map/{idf}/$", MarkerViewSet)
#router.register(r"^map/(?P<id_f>[0-9]+)/$", MarkerViewSet)
router.register(r"map/", MarkerViewSet)
urlpatterns = router.urls
because I have to pass my id filed parameter, isn’t?
But I am confused where should I import from map.viewsets import MarkerViewSet
I do not really understand your sentence
No, you need to import it in your urls.py file from the file in which it currently resides.
If I past from map.viewsets import MarkerViewSet
to my project/urls.py I have this error
TypeError: The actions
argument must be provided when calling .as_view()
on a ViewSet. For example `.as_view({‘get’: ‘list’})
but now, with that, I get no errors
path("api/map/<int:idfield>/", MarkerViewSet.as_view({'get':'list'}), name="marker_view_set")
But better IT’S WORK!!!
but this {'get':'list'}
is correct??? I am really confused with get and list
Why I am saying it work. Give me a short time to describe it.
First, in my map.js file, I have this
async function load_markers() {
//const markers_url = `/api/map/?in_bbox=${map.getBounds().toBBoxString()}`;
const markers_url = `/api/map/4/`; // I CHANGE 4 to 2 and to 1 and reload the page
console.log("markers_url: ",markers_url);
const response = await fetch(markers_url);
//console.log("response: ",response);
const geojson = await response.json();
console.log("geojson: ",geojson);
return geojson;
}
in my project/urls.py I added this
from django.contrib import admin
from django.urls import path, include
from map.viewsets import MarkerViewSet
urlpatterns = [
path('admin/', admin.site.urls),
path('map/', include('map.urls')),
path('', include('map.urls')),
#path("api/", include("map.api")),
#path("api/map/<int:idf>/", include("map.api")),
path("api/map/<int:idfield>/", MarkerViewSet.as_view({'get':'list'}), name="marker_view_set")
#path("api/{idf}", include("map.api")),
]
Is it what your are expected me to do, isn’t?
in my api.py file, I have this
router = routers.DefaultRouter()
#router.register(r"map", MarkerViewSet) #!!!?????
#router.register(r"^map/{idf}/$", MarkerViewSet)
#router.register(r"^map/(?P<id_f>[0-9]+)/$", MarkerViewSet)
router.register(r"map/", MarkerViewSet)
urlpatterns = router.urls
I kept the original line, is really correct?
Then in my viewsets file, I have this
from rest_framework import viewsets
from rest_framework_gis import filters
from map.models import Stations
from map.serializers import StationsSerializer
class MarkerViewSet(viewsets.ReadOnlyModelViewSet):
"""Marker view set."""
bbox_filter_field = "location"
filter_backends = (filters.InBBoxFilter,)
queryset = Stations.objects.filter(station_active=1, map=1)
serializer_class = StationsSerializer
"""
def get_tags(self):
return Stations.objects.filter(fields_id_field=1)
"""
def get_queryset(self):
print("iii",self.kwargs['idfield'])
#print("ppp:", self.kwargs['idfield'])
return Stations.objects.filter(fields_id_field=2)
the line print("iii",self.kwargs['idfield'])
prints iii 1 when const markers_url =
/api/map/1/;
prints iii 2 when const markers_url =
/api/map/2/;
prints iii 4 when const markers_url =
/api/map/4/;
and I tried
prints iii 40 when const markers_url =
/api/map/40/;
Now I modified this
class MarkerViewSet(viewsets.ReadOnlyModelViewSet):
"""Marker view set."""
bbox_filter_field = "location"
filter_backends = (filters.InBBoxFilter,)
queryset = Stations.objects.filter(station_active=1, map=1)
serializer_class = StationsSerializer
"""
def get_tags(self):
return Stations.objects.filter(fields_id_field=1)
"""
def get_queryset(self):
print("iii",self.kwargs['idfield'])
idf = self.kwargs['idfield']
#print("ppp:", self.kwargs['idfield'])
return Stations.objects.filter(fields_id_field=idf)
and when I change the value 1 to 2 to 4 const markers_url =
/api/map/1/;
and I refresh the page, it’s work. The map show the correct field!!! Houhaaa!!
But I am still not confident, if my code is well coded
router = routers.DefaultRouter()
router.register(r"map/", MarkerViewSet)
urlpatterns = router.urls
Why that does not generate an error?
Is there a way to make it better?
Why this does not generate an error ({'get':'list'})
. Could you clarify me that? Is it really correct?