Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
filter users within a given distance in Django rest framework

I'm trying to implement a custom filter in Django rest framework, which returns the users within a given distance, I'm using Django filters. I am assuming that latitude and longitude are already provided by front end.

this what I tried. I don't know if it's the right way to do it. I'm having the following error

'RenameAttributes' object is not iterable

Here is my code

Views.py

def calculateDistance(lon1, lat1, lon2, lat2):
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    return 6371 * (
        acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon1 - lon2))
    )
class CustomFilter(django_filters.rest_framework.DjangoFilterBackend):
    def filter_queryset(self, request , queryset,view):
        alluser = queryset
        newQueryset = []
        distparam = request.GET.get('distance')
        if distparam and bool(int(distparam)):
            for user in alluser:
                current_user_long = request.user.Longitude
                current_user_lat = request.user.Latitude
                alluser_long = user.Longitude
                alluser_lat = user.Latitude
                distance = calculateDistance(current_user_long, current_user_lat, alluser_long, alluser_lat)
                if distance > distparam:
                    newQueryset.push(user)
            return newQueryset
        return queryset
class UserFilter(filters.FilterSet):
    class Meta:
        model = User
        fields =['gender', 'last_name', 'first_name' ]
class UserListView(generics.ListAPIView):

    queryset = User.objects.all()
    serializer_class = UserSerializer
    filterset_class = UserFilter
    filter_backends = CustomFilter

Models.py

class User(AbstractUser):
   username = None
   email = models.EmailField(
       max_length=100, verbose_name='email', unique=True)
   gender = models.CharField(
       max_length = 20,
       choices = GENDER_CHOICES,
       default = 'М'
       )  
   avatar= ProcessedImageField(upload_to='avatars',
                                          processors=[ResizeToFill(400, 400), Watermark()],
                                          format='JPEG',
                                          options={'quality': 72})    
   likes = models.ManyToManyField('User', blank=True, related_name="like")
   latitude = models.DecimalField(max_digits=22, decimal_places=16, blank=True, null=True)
   longitude = models.DecimalField(max_digits=22, decimal_places=16, blank=True, null=True)
   USERNAME_FIELD = 'email'
   REQUIRED_FIELDS = []
   objects = UserManager()

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think the error appearing here is not related to distance calculation/filtering, it might be incorrect settings Please make sure that DEFAULT_FILTER_BACKENDS is a valid tuple or a list

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!