python - Django Looping through QuerySet Objects and adding temporary field to use for filtering -
i'm using djangorestframework , have queryset returns objects generic listapiview. want loop through every object in queryset , run code below calculate distance value using geopy distance.distance , add together temporary field can later filter queryset based on new distance field.
currently have next code, doesn't seem save new temporary field address.distance, homecoming new queryset while showing new distance value each object
in views.py:
class business_list(generics.listapiview): serializer_class = businessserializer @csrf_exempt def get_queryset(self): """ list businesses. """ queryset = business.objects.all() distanceparam = self.request.query_params.get('distance', none) locationparam = self.request.query_params.get('location', none) if distanceparam not none , locationparam not none: business in queryset: storelocations = () location in business.locations.all(): long = location.address.all()[:1].get().long lat = location.address.all()[:1].get().lat businesscoords = (lat, long) testcoords = (40.795745, -73.948646) address = location.address.all()[:1].get() address_two = location.address.all()[:1].get() address.distance = distance.distance(businesscoords, testcoords).miles # my_obj_list = list(address, address_two) # queryset = my_obj_list # wd = business.objects.annotate(distance="lol") # queryset = wd homecoming queryset
python django django-queryset
No comments:
Post a Comment