So here's my nice function:
Checks if an address can be serviced by an Employee.
Returns True if address' lat-lng intersects with any of Employees' coverage.
If it doesn't, but address' locality is listed in world District model,
returns False and locality name.
Just returns False if nothing found.
-- address -- django-address Address object. Address should be geocoded (i.e
have latitude and longitude fields correctly filled)
pnt = Point(address.longitude, address.latitude)
employee_exists = Employee.objects.filter(coverage__mpoly__intersects=pnt).exists()
district = District.objects.filter(mpoly__intersects=pnt).first()
return False, district.name
coverage = models.ManyToManyField(
mpoly = models.MultiPolygonField()
You could do an annotation on the count of the employees and then filter on the count of that
You may also want to use
.only('name') since that is all you actually use in the district.
Disclaimer: This is untested and I have no idea i this actually will be any faster since
exists isn't very intensive