简体   繁体   中英

how to print a result of a calculation in django rest framework

so in my project i am calculating the distance between 1 user and a list of users but for some reason it is not printing nore displaying the result of the calculation below is my api view

class Listsellers(generics.ListAPIView):
    authentication_classes = [SessionAuthentication, BasicAuthentication]
    queryset = Seller_account.objects.all()
    serializer_class = SellerAccountSerializer
    filter_backends = [DjangoFilterBackend]
    def calc_distance(self, lat_a, long_a, lat_b, long_b):
        EARTH_RADIUS_IN_MILES = 3958.761
        """all angles in degrees, result in miles"""
        lat_ab = radians(lat_a)
        lat_b = radians(lat_b)
        delta_long = radians(long_a - long_b)
        cos_x = (
               sin(lat_ab) * sin(lat_b) +
               cos(lat_ab) * cos(lat_b) * cos(delta_long)
            )
        return acos(cos_x) * EARTH_RADIUS_IN_MILES
    def calc_dist(self):
        user_long = self.request.Users.objects.username.longitude
        user_lat = self.request.Users.objects.username.latitude
        seller_lat = Seller_account.objects.latitude
        seller_long = Seller_account.objects.latitude
        lat_a = user_lat
        print(lat_a)
        long_a = user_long
        print(long_a)
        lat_b = seller_lat
        print(lat_b)
        long_b = seller_long
        print(long_b)
        distance = Users.objects.calc_distance(self, lat_a, long_a, lat_b, long_b)
        print(distance) 

is there something i am missing?

You can add get_queryset() to your api view.

class Listsellers(generics.ListAPIView):
    authentication_classes = [SessionAuthentication, BasicAuthentication]
    # queryset = Seller_account.objects.all()
    serializer_class = SellerAccountSerializer
    filter_backends = [DjangoFilterBackend]
    def calc_distance(self, lat_a, long_a, lat_b, long_b):
        EARTH_RADIUS_IN_MILES = 3958.761
        """all angles in degrees, result in miles"""
        lat_ab = radians(lat_a)
        lat_b = radians(lat_b)
        delta_long = radians(long_a - long_b)
        cos_x = (
               sin(lat_ab) * sin(lat_b) +
               cos(lat_ab) * cos(lat_b) * cos(delta_long)
            )
        return acos(cos_x) * EARTH_RADIUS_IN_MILES
    def calc_dist(self):
        user_long = self.request.Users.objects.username.longitude
        user_lat = self.request.Users.objects.username.latitude
        seller_lat = Seller_account.objects.latitude
        seller_long = Seller_account.objects.latitude
        lat_a = user_lat
        print(lat_a)
        long_a = user_long
        print(long_a)
        lat_b = seller_lat
        print(lat_b)
        long_b = seller_long
        print(long_b)
        distance = Users.objects.calc_distance(self, lat_a, long_a, lat_b, long_b)
        print(distance) 
    
    def get_queryset(self):
        queryset = Seller_account.objects.all()
        self.calc_dist()
        return queryset

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM