fix: avoid DB access during app initialization in labels_manager seri… - #9880
Open
nikitabelonogov wants to merge 1 commit into
Open
fix: avoid DB access during app initialization in labels_manager seri…#9880nikitabelonogov wants to merge 1 commit into
nikitabelonogov wants to merge 1 commit into
Conversation
…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
❌ Deploy Preview for label-studio-playground failed. Why did it fail? →
|
✅ Deploy Preview for heartex-docs canceled.
|
✅ Deploy Preview for label-studio-docs-new-theme canceled.
|
❌ Deploy Preview for label-studio-storybook failed. Why did it fail? →
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, twoPrimaryKeyRelatedFieldfields weredeclared with
queryset=Project.objects.all()at class level. Since classattributes are evaluated at import time, which happens during
AppConfig.ready(), this triggered a SQL query before the database wasready, violating Django 5.1's expected app lifecycle.
Fix
Replaced the eager QuerySet with a lazy Manager in both occurrences:
DRF accepts both QuerySet and Manager in
PrimaryKeyRelatedField. When givena Manager, it calls
get_queryset()internally only at request validationtime, 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