I have the following model:
class Artist(models.Model):
cell_phone = models.CharField(20)
pr_phone = modelsCharField(20)
In my template, I can search for all artists for whom the entered phone number match their cell phone or PR phone. Assuming there are no restrictions on how the phone numbers have been initially entered, that is phone number can be stored in any of the following formats:
123 456 7890
(123) 456-7890
123-456-7890
The brute force way to do it is:
# variable input_phone has already been stripped of dashes, braces and spaces
all_artists = Artists.objects.all()
artists = []
for artist in all_artists:
cell_phone = artist.cell_phone
pr_phone = artist.pr_phone
if cell_phone.find(input_phone) >= 0 or pr_phone.find(pr_phone) >= 0:
artists.append(artist)
return artists
I'm using find
because I can also search for all artists who live within a specific area code. Is there another way to the above through Django's query filter?