I want to make an annotate and append a new value, but this one will depend on the query.
Inspeccion.objects.filter(llanta_id = id).last()
It would be as it follows:
Tire.objects.all().annotate(
inspeccion = Inspeccion.objects.filter( llanta_id = F('id') ).last()
)
My main gosl is that the id comes out directly from the tire model, i have tried several ways but none of them work. Could you help me, please?
My models is:
class Tire(models.Model):
# Modelo de la Llanta
economic_numer = models.CharField(max_length=200, null=True)
compania = models.ForeignKey(Compania, on_delete=models.CASCADE, null=True, blank=True)
vehicle = models.ForeignKey(Vehiculo, on_delete=models.CASCADE, null=True, blank=True)
type_of_eje = models.CharField(max_length=4, null=True, blank=True)
eje = models.IntegerField(blank=True, null=True)
position = models.CharField(max_length=4, null=True, blank=True)
name_of_eje = models.CharField(max_length=200, choices=opciones_de_eje, null=True, blank=True)
class Inspeccion(models.Model):
# Modelo de la Inspección
opciones_evento = (("Inspección", "Inspección"),
("Inspección Galgo", "Inspección Galgo")
)
type_event= models.CharField(max_length=1000, choices=opciones_evento)
tire= models.ForeignKey(Tire, on_delete=models.CASCADE)
position= models.CharField(max_length=4, null=True, blank=True)
type_of_eje = models.CharField(max_length=4, null=True, blank=True)
eje = models.IntegerField(blank=True, null=True)
user = models.ForeignKey(Perfil, on_delete=models.CASCADE, null=True, blank=True)
date= models.DateTimeField(auto_now=True, null=True, blank=True)
left_depth= models.FloatField(blank=True, null=True)
center_depth= models.FloatField(blank=True, null=True)
right_depth= models.FloatField(blank=True, null=True)