I know that object_id
is more efficient than object.id
but is this rule working for ForeignKey filtering ?
Is
Model.objects.filter(author_id=author_obj.id)
or
Model.objects.filter(author_id=author_id)
more efficient than
Model.objects.filter(author=author_obj)
As stated in Queries over related objects section of documentation there's no difference between author_obj.id
and author_obj
:
For example, if you have a Blog object b with id=5, the following three queries would be identical:
Entry.objects.filter(blog=b) # Query using object instance
Entry.objects.filter(blog=b.id) # Query using id from instance
Entry.objects.filter(blog=5) # Query using id directly
Personally I use entry.blog_id
as a rule in my projects as it does not generate extra query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With