I have not found this question on SO.
Is there a speed difference between the following two variants of doing foreign key lookup in django's object model? I only need the foreign key.
class A(models.Model):
x = models.PositiveIntegerField(null=True)
class B(models.Model):
a = models.ForeignKey(A, null=True, on_delete=models.CASCADE)
y = models.PositiveIntegerField(null=True)
b = b.objects.last()
Which is faster or are they the same?
b.a_id
b.a.id
edit: Actually I think I can answer it myself... the second variant should be slower since it creates the whole object from the db, right?