I have the following models:
class Videos(models.Model):
title = models.CharField(max_length=45)
class TimelinePoints(models.Model):
description = models.CharField(max_length=45)
videos_id = models.ForeignKey('Videos', models.DO_NOTHING, db_column='Videos_id')
# ... Otros campos sin importancia
I want to do a search by bringing all the videos whose title contains the string entered by the user, and all the videos whose TimelinePoint
also contain that string in their description field.
Until now I was solving it in the following way:
videos = Videos.objects.filter(Q(title__icontains = string_ingresado) | Q(timelinepoints__description__icontains = string_ingresado))
But later I realized that my application needs to have the list of descriptions of TimelinePoints
matched, so I had to add this query below:
timeline_points = TimelinePoints.objects.filter(description__icontains = string_ingresado).only('description')
This does not convince me much since I am doing an expensive process 2 times. So my question is:
Is there a way to do what I need without having to do the same search twice?