2
I’m looking to create a queryset that returns only the cars that have had the last approved review. It is known that there may be n cars, and each car has a history of revisions, so there may be n revisions for each car.
class Carro(models.Model):
marca = models.CharField(max_length=100)
class Revisao(models.Model):
carro = models.ForeignKey(Carro)
data = models.DateField()
aprovado = models.BooleanField(default=False)
I am currently performing a not very performatic algorithm, with the following logic:
lista_de_aprovados = []
for carro in Carro.objects.filter(revisao=True):
try:
carro_aprovado = carro.revisao_set.filter(aprovado=True).latest('data')
except ObjectsDoesNotExist:
pass
else:
lista_de_aprovados.append(carro_aprovado)
carros_aprovados = Carros.objects.filter(id__in=[l.pk for l in lista_de_aprovados])
Is there a way to accomplish this with just a queryset? I’m using Django 1.8 and the problem is that I’m realizing that same logic more than once and it’s slowing down a bit when it comes to loading the page. As I said earlier, the idea is a queryset that returns only the cars that had the last approved revision.
Carros.objects.filter(revisao=True).filter(...logica)
When you say "the last approved review" does that mean there can be approved reviews, with another subsequent unapproved review? Or is the approved one always the last? I gave an answer based on the first scenario - which also works for the second, of course, but is sub-optimal in this case (just see if there is some approved review...).
– mgibsonbr