I've got a django model that has a custom attribute called LocationField
.
class List(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=200)
location = LocationField(blank=True, max_length=255)
The values in this are stored as a string of format latitude, longitude
. From my template, I pass a url as follows: /nearby?lat='+somevalue+'&long='+somevalue
Now, I want to return nearby entries from List
depending on the values that are passed.
For this I've written a views.py function as follows:
def nearby(request):
if request.GET['lat']:
lat = request.GET['lat']
longitude = request.GET['long']
first_query = Playlist.objects.filter(location__istartswith=lat)
for f in first_query:
l = f.index(',')
n_string = f[l:]
To clarify what I've done, first_query
returns all entries that start with the same latitude
. However, now I also want to match the longitude
which is why I'm running that for loop
and searching for the index of the comma that separates latitude,longitude
in my LocationField
. n_string
takes the substring of the LocationField
and I'm planning to then match it to my longitude
variable.
My question is two part:
- How do I generate the query for matching the latitude and how do I return it to template?
- How do I check, in say, an area of 2 sq.km around that area?
Are there django packages for this?