Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

which I should prefer user.address.id or user.address_id in django?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading