I have two models User and Address, User has a foreign key relationship with Address model, I want to get the Address id from the user object, which query I should prefer, or is there any other efficient way?
user = User.objects.last()
we have two queries which one is efficient:
user.address.id
or
user.address_id
Thanks
>Solution :
In Django we have two options, if you use user.address.id it will hit the database and produce the result.
If you are querying more you must use user.address_id, as Django has already cached on the object.