trex
trex

Reputation: 325

GeoDjango distance query ForeignKey

I have a geCoded Address model and I have a user model has Address as foreignKey. I can perform a distance calculation within the Address model easily by using the following

Address.objects.filter(geoCoords__distance_lt=(address1.geoCoords, D(mi=23)))

Address Model:

class Address(models.Model):

   streetAddress1 = models.CharField(blank=False, null=False, max_length=300)
   city = models.CharField(null=False, blank=False,  max_length=50)
   state = USStateField(choices = US_STATES, blank=False, null=False,)
   zip5 = models.CharField(blank=False, null=False, max_length=5, )
   zip4 = models.CharField(blank=True, null=True, max_length=4, )
   geoCoords = models.PointField(null=False, blank=False, srid=4326)
   objects = models.GeoManager()

User Model:

class TestUsers
   ...
   location = models.ForeignKey(Address, null=True, blank=True)
   ...

I'd like to perform a query to get all TestUsers within certain distance of a pointField. Any help is appreciated.

Upvotes: 0

Views: 155

Answers (2)

trex
trex

Reputation: 325

Needed to add following to the model that is using Address as a ForeignKey

objects = models.GeoManager() 

Once that is done, the following query resolves properly:

TestUsers.objects.filter(location__geoCoords__distance_lte=(caddr.geoCoords, D(mi=21)))

Upvotes: 0

Sachi Tekina
Sachi Tekina

Reputation: 1810

Try this:

TestUsers.objects.filter(location__geoCoords__distance_lt=(address1, D(mi=23)))

Upvotes: 1

Related Questions