Context: I have a database of houses which users can query by location name (e.g. 'New York, NY' or '100 1st Street, San Francisco'). I'm using Google Maps API to retrieve pinpoints on a map of each of the query results, in addition to a list of each of the objects. Am using Django and Postgres as my framework and DB respectively.
Problem: I'm wondering how to filter the House objects by their location, but not all location queries will contain the same information (i.e. some may have a house number, some may have a city or state, and some may not).
As shown by the code below, every House object is linked via a OneToOneField to a Location object which contains the necessary fields.
This is also complicated by the fact that every Location object is made up of several fields, whereas the query will be a string that might not match a single field as you would use in Django's filter() method. A query such as '100 1st Street, San Francisco' doesn't match any of the individual Location fields since this query is made up of several fields. How might I write an algorithm of sorts to find any objects that match a given query as described?
Code:
models.py:
class House(models.Model):
...
mapped_location = models.OneToOneField(Location, related_name='location_house')
...
class Location(models.Model):
...
name = models.CharField(...)
street_name = models.CharField(...)
city = models.CharField(...)
views.py:
def show_results(request):
House.objects.filter( ??? )
return render(request, 'results.html', context)
Let me know if I need post anymore code, thanks!