Skip to content

fix: avoid DB access during app initialization in labels_manager seri… - #9880

Open
nikitabelonogov wants to merge 1 commit into
developfrom
pr6/issue
Open

fix: avoid DB access during app initialization in labels_manager seri…#9880
nikitabelonogov wants to merge 1 commit into
developfrom
pr6/issue

Conversation

@nikitabelonogov

Copy link
Copy Markdown
Member

Description

Fixes the RuntimeWarning reported in issue #9715, where Django 5.1 emitted
"Accessing the database during app initialization is discouraged" on server startup.

Fixes #9715

Root Cause

In labels_manager/serializers.py, two PrimaryKeyRelatedField fields were
declared with queryset=Project.objects.all() at class level. Since class
attributes are evaluated at import time, which happens during
AppConfig.ready(), this triggered a SQL query before the database was
ready, violating Django 5.1's expected app lifecycle.

Fix

Replaced the eager QuerySet with a lazy Manager in both occurrences:

# Before
project = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())

# After
project = serializers.PrimaryKeyRelatedField(queryset=Project.objects)

DRF accepts both QuerySet and Manager in PrimaryKeyRelatedField. When given
a Manager, it calls get_queryset() internally only at request validation
time, eliminating the database access during app initialization.

Testing

Server started with python -W error::RuntimeWarning manage.py runserver,
no RuntimeWarning emitted after the fix.

This PR is a mirror for #9825

…alizers: PrimaryKeyRelatedField with queryset=Project.objects.all() was being evaluated at module import time, triggering a DB query before Django's app registry was ready and causing RuntimeWarning on startup (Django 5.1). Replaced with queryset=Project.objects so DRF evaluates the queryset lazily at request time via its get_queryset() method. Fixes #9715
@netlify

netlify Bot commented Aug 5, 2026

Copy link
Copy Markdown

Deploy Preview for label-studio-playground failed. Why did it fail? →

Name Link
🔨 Latest commit 2c7141f
🔍 Latest deploy log https://app.netlify.com/projects/label-studio-playground/deploys/6a73a4f141e3870008693117

@netlify

netlify Bot commented Aug 5, 2026

Copy link
Copy Markdown

Deploy Preview for heartex-docs canceled.

Name Link
🔨 Latest commit 2c7141f
🔍 Latest deploy log https://app.netlify.com/projects/heartex-docs/deploys/6a73a4f10aa617000832a27c

@github-actions github-actions Bot added the fix label Aug 5, 2026
@netlify

netlify Bot commented Aug 5, 2026

Copy link
Copy Markdown

Deploy Preview for label-studio-docs-new-theme canceled.

Name Link
🔨 Latest commit 2c7141f
🔍 Latest deploy log https://app.netlify.com/projects/label-studio-docs-new-theme/deploys/6a73a4f19f71ce0008e87bf5

@netlify

netlify Bot commented Aug 5, 2026

Copy link
Copy Markdown

Deploy Preview for label-studio-storybook failed. Why did it fail? →

Name Link
🔨 Latest commit 2c7141f
🔍 Latest deploy log https://app.netlify.com/projects/label-studio-storybook/deploys/6a73a4f172e979000841f87c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RuntimeWarning: Accessing the database during app initialization on Docker startup (Django 5.1

2 participants