Not using foreign keys directly

When working with foreign keys, accessing the related field will result in a database read. That can be eliminated by using *_id, which is the foreign key value that Django has already cached on the object to make this scenario more efficient.

If we spot this issue in your GitHub pull request we give this advice:

tasks.pyperformancemedium
def trigger_tasks(queryset):
    for item in queryset:
-
        run_async_task(item.page.id)
+
        run_async_task(item.page_id)

When working with foreign keys, accessing the related field will result in a database read. That can be eliminated by using *_id, which is the foreign key value that Django has already cached on the object to make this scenario more efficient.

Read more
Protect your pull requests from over 40 types of common Django technical debts with our GitHub code review bot.

Configuring this check

Django Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code indirect-foreign-key in your pyproject.toml file.

Read more about configuring Django Doctor.