If I have a instance of an object, can I get the ID of a related object without needing to access the database? The value must be storred somewhere.
eg
book = Book.objects.get(id=1)
book.author.id
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Django automatically creates a field for the id of the object by using the field name and suffixing it with _id eg:
It’s not very well documented in django’s docs but two blog posts on the matter for reference:
http://www.yilmazhuseyin.com/blog/dev/django-orm-performance-tips-part-2/
http://thebuild.com/blog/2010/12/22/getting-the-id-of-related-objects-in-django/