Django QuerySet access foreign key field directly, without forcing a join -


suppose have model entry, field "author" pointing model author. suppose field can null.

if run following queryset:

entry.objects.filter(author=x)

where x value. suppose in mysql have setup compound index on entry other column , author_id, ideally i'd sql use "author_id" on entry model, can use compound index.

it turns out entry.objects.filter(author=5) work, no join done. but, if author=none, django join author, add clause author.id null. in case, can't use compound index.

is there way tell django check pk, , not follow link?

the way know add additional .extra(where=['author_id null']) queryset, hoping magic in .filter() work.

thanks.

(sorry not clearer earlier this, , answers lazerscience , josh).

does not work expected?

entry.objects.filter(author=x.id) 

you can either use model or model id in foreign key filter. can't check right yet if executes separate query, though i'd hope wouldn't.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -