Search code examples
pythondjangogeodjango

django use column value for filtering


I have a table of points with location data as stored in the following columns:

coordinates: the geolocation data in longitude and latitude
radius:      the distance from coordinate that it is visible

Now I take a random point and want to find the points that are visible to the random point using django geolocation such as:

LocationInfo.objects.filter(coordinates__distance_lt=(random_point, D(km=1))

Currently I am retrieving the points within 1 km radius but I want to use the radius from the table. How can I use the radius from the table?


Solution

  • filters-can-reference-fields-on-the-model

    from django.db.models import F
    
    LocationInfo.objects.filter(
        coordinates__distance_lt=(random_point, D(km=F('FIELD_NAME'))
    )
    #                                               ^^^^^^^