Working on a last-minute mapping app for PyCon here, I'm currently using a raw SQL query to SELECT information from People (Model) who all share the same Tag (Model in a ManyToMany with People). I'm feeding the query a range of lat/lon and the tag I want,
c = db.cursor() c.execute("""select p.id, p.name, p.lat, p.lon from maps_people as p left join maps_tags_person as pt on pt.person_id = p.id left join maps_tags as t on t.id = pt.tag_id where t.tag = '%s' and p.lat >= %s and p.lon >= %s and p.lat <= %s and p.lon <= %s order by p.id""" % (request_tag, minlat, minlon, maxlat, maxlon)) r = c.fetchall() c.close() I'd just like to figure out the Django API equivalent (if there is one) to do something like this. I was thinking, like, people.get_list(lat__gte=minlat, lon__gte=minlon, lat__lte=maxlat, lon__lte=maxlon, has__tag=tag) or whatever... ideas? --- Second question, What is a good method of keeping a count of ManyToMany relationships that each entry in the table has? In the example above, I added a meta.IntegerField to the Tag Model, and I want it to hold the # of people that have that tag. I don't want to calculate this as-needed (way to often, if I use it)... I can't really import the model IN the model, can I? (I tried, and then my brain exploded) My original thought was to just add a _pre_save that updated with the count everytime a Tag was saved (Tags aren't really edited, so pretty much ALL saves affect the number of relationships) ... but I wasn't sure what a "best practice" method of this was. --- Thanks, Brett