I have the following Django rest framework model:
from django.db import models
from django.utils import timezone
from Project_Level.CONSTANTS import AREAS
class KnownLocation(models.Model):
name = models.CharField(name="Name",
unique=False,
max_length=150,
blank=False,
help_text="Enter the name of the location's name")
area = models.CharField(name='Area',
max_length=8,
choices=AREAS)
date_added = models.DateTimeField(default=timezone.now)
latitude = models.FloatField(name="Latitude",
unique=True, max_length=255, blank=False,
help_text="Enter the location's Latitude, first when extracting from Google Maps.",
default=1)
longitude = models.FloatField(name="Longitude",
unique=True, max_length=255, blank=False,
help_text="Enter the location's Longitude, second when extracting from Google Maps.",
default=1)
AREAS = [
('210', '210'),
('769', '769'),
('300', '300')
]
serializer:
from rest_framework.serializers import Serializer
from .models import KnownLocation
class KnownLocationSerializer(Serializer):
class Meta:
model = KnownLocation
fields = ('id', 'Name', 'Area', 'Latitude', 'Longitude')
I want to write a view with a query set ( maybe a get_queryset method will be better), where the query return all objects which has the same 'area' has the one past in by the user.
in views.py
> @api_views (['GET']) def filterArea_KnownLocation(request, area):
> locations = KnownLocation.objects.filter(area=area)
> serializer = KnownLocationSerializer(locations, many=True, context={'request': request})
> return Response(serializer.data, status=status.HTTP_200_OK)
in urls.py
path('KnownLocations/filter/area/<str:area>', views.filterArea_KnownLocation)
now for example, if you are using vue in front-end you can do the following:
getLocationsByArea(){
this.$http
.get("/KnownLocations/filter/area/210", {
headers: {
"Content-Type": "application/json",
}
})
.then((res)=>{
this.locations = res.data;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.