I have a geographic object that is used in proximity searches. That object is keyed to different other objects via various paths.
Examples:
blog.geo_object
user.profile.geo_object
group.event.geo_object
Right now, i do a bounding box search, which strings like this:
radius_queryset = base_queryset.filter(
user__profile__geo__lat__gte = bounding_box.lat_min,
user__profile__geo__lat__lte = bounding_box.lat_max,
user__profile__geo__lon__gte = bounding_box.lon_min,
user__profile__geo__lon__lte = bounding_box.lon_max,
and then on other objects:
radius_queryset = base_queryset.filter(
blog__geo__lat__gte = bounding_box.lat_min,
blog__geo__lat__lte = bounding_box.lat_max,
blog__geo__lon__gte = bounding_box.lon_min,
blog__geo__lon__lte = bounding_box.lon_max,
)
This follows a general format of:
radius_queryset = base_queryset.filter(
[lookup_path]__geo__lat__gte = bounding_box.lat_min,
[lookup_path]__geo__lat__lte = bounding_box.lat_max,
[lookup_path]__geo__lon__gte = bounding_box.lon_min,
[lookup_path]__geo__lon__lte = bounding_box.lon_max,
)
# where lookup_path = "blog" or "user__profile" in the two above examples
I’m writing enough of these (3 so far, more to come) to want to generalize the queries — encapsulation being a good friend of maintainability and an ally in the fight against typo-bugs.
So, to my question: short of using exec and eval (which just look ugly), is there a way to get the filter parameter name to sub-in a variable? Am I missing something simple here?
**kwargsis your answer!