# How to design model in Django using GeoDjango?

I am using Postgresql 9.0 and PostGIS 2.0. I am using Django 1.3 and GeoDjango.I have model with id,latitude,longitude and placename. All the places are in USA.

My requirement is following -

User will give his address. We use his address to get latitude and longitude, and then find places within 'x' miles.

So, should I use PointField in the model? Also, how do I convert users latitude\longitude into geometry object to use dwithin?

-

Your model must be like this:

# Import django module
from django.contrib.gis.db import models

class Point(models.Model):
placename = models.CharField(max_length=50)
geom = models.PointField(srid=4326)
objects = models.GeoManager()

def __unicode__(self):
return '%s %s %s' % (self.name, self.geom.x, self.geom.y)


from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib.gis.measure import D # D is a shortcut for Distance

pnt = fromstr(str(po), srid=4326)
qs = Point.objects.filter(point__distance_lte=(pnt, D(km=5)))

return HttpResponse(your choose for returning object)


it will give you objects in 5 km square.

Geodjango Distance Lookups
Availability: PostGIS, Oracle, SpatiaLite

The following distance lookups are available:

distance_lt
distance_lte
distance_gt
distance_gte
dwithin


Dont forget, qs is in array and it filter all objects for your request that you have to loop for getting all objects one by one..

Now you can convert your result to geojson or anything for your app...

i hope it helps you....

-
I have already done this but getting error when firing distance queries –  carlsen Apr 26 '12 at 6:28
What errors are you getting? –  blah238 Apr 26 '12 at 6:34
first of all, you have to make geometry column in your db from your x and y. because of spaial query, you will need this...and give me some detail about your error... –  Aragon Apr 26 '12 at 6:46
Very helpful, thanks! –  Mark Bell Mar 23 '13 at 9:31