It is possible to perform a query within an annotate?

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:

    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)

What are you actually trying to achieve or determine here? What is your ultimate objective?

Hello and thank you for answering, the context is as follows, the application I am working on is about managing the tires of some vehicles, and follow their life over time. These tires can have an inspection, which is to record the state of the tire at the time that such action was performed.
A tire can have many inspections, but for this case I only need the last inspection that was assigned, and the only way to relate it is through the Inspection since it has an FK to the tire.
So I want to know what is the last inspection of the tire, but as there are thousands of tires I considered that the best way to do it was to do it in an annotate.

It would help if you were to post the relevent portions of the models, but to get started, I’ll make whatever assumptions necessary.

The objective is that you want a list of all Tire with a reference to the most recent Inspection.

I’m going to assume that Inspection has a datetime field named date_inspected such that we know what the last inspection is.

What you haven’t specified is what data you need from that inspection - the solution is going to be different depending upon whether you need just a field or two, or if you need a reference to the complete object.

If you just need one or two fields, then yes, an annotation would be appropriate. However, if you really need the complete object, then an annotation is not going to do you any good. (All an annotation would do for you is to give you the pk of the related object - you can’t annotate a complete object. You would still need to issue a query for each tire to retrieve the related inspection.)

If you have an instance of Tire named tire, then tire.inspection_set.order_by('-date_inspected').first() is the most recent inspection.

However, this is still going to be an N + 1 query situation, so you’ll want do use the prefetch_related clause on your original Tire query to reduce the number of queries to 2.

An apology for omitting the models, I updated the question adding the relevant parts of the model, and actually the only thing I need are the fields “left_depth”, “center_depth”, “right_depth”, but as I mentioned, there is no relationship directly from Tire, since who has the FK is the Inspection.
Once I have those 3 fields I have to get the smallest of them, which I have done before, my only problem is to get that data from the inspection but consulting the tire.

Yes there is. You always have the “reverse foreign key” relationship available to you.


Thank you very much, I will read the information you provided, and update when I get it resolved.
Thank you for taking the time to answer me, and I apologize for the mistakes in writing the question.