I’m a Django beginner. Most tutorials I’ve read so far haven’t included logic that’s much more complicated than basic database queries. I’m working on a website and the first feature I’m trying to code is a function that accepts a list of strings, and searches virtually every field of a model for that string, and returns a queryset for each search term.
I was going to make a services.py file to put the function in, then I read a blog sternly warning against it, though I’m not sure I fully understood it. Am I supposed to put basically any function that’s semi-complicated in a Model or ModelManager?
Here’s what I have, which works, but I just want to make sure I’m starting off with good code practices (the last search_field is to a model with a foreign key to self.model):
class MoleculeManager(models.Manager):
def _build_search_query(self, search_string, molecule_types):
search_fields = [
'hgnc_id',
'hgnc_symbol',
'hgnc_name',
'hmdb_accession',
'hmdb_name',
'moleculealias__alias',
]
query = Q()
for search_field in search_fields:
query |= Q(**{f"{search_field}__iexact": search_string})
return query & Q(type__in=molecule_types)
def find_matching_molecules(self, search_strings, molecule_types):
"""Takes list of strings to search on and list of molecule types (e.g. Molecule.MoleculeType.GENE) to search"""
search_results = []
for search_string in search_strings:
query = self._build_search_query(search_string, molecule_types)
molecules = self.filter(query).distinct()
search_results.append({'search_string': search_string, 'molecules': molecules})
return search_results