Skip to content

Commit

Permalink
refactor: move get_user_model call out of method
Browse files Browse the repository at this point in the history
The get_service_user method used to do a local call to the
get_user_model function because it was not guaranteed to be properly
initialized at the time of import. This was partly due to how we did
custom initialization using lms/startup.py, but it was also because
when it was implemented (commit f318661), the platform was still
running on Django 1.8.18. At that time, get_user_model was guaranteed to
work only after Django has imported all models.

In Django 1.11, the behavior of get_user_model was changed:

  https://docs.djangoproject.com/en/1.11/releases/1.11/#django-contrib-auth

> get_user_model() can now be called at import time,
> even in modules that define models.

Now that lms/startup.py is gone and get_user_model is safe to call at
the module level, I'm refactoring the catalog app's models.py file to
follow the convention we use everywhere else in edx-platform with
respect to get_user_model.
  • Loading branch information
ormsbee committed Mar 1, 2025
1 parent c4f21b6 commit d5aa834
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions openedx/core/djangoapps/catalog/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Models governing integration with the catalog service."""


from config_models.models import ConfigurationModel
from django.conf import settings
from django.contrib.auth import get_user_model
Expand All @@ -9,6 +7,8 @@

from openedx.core.djangoapps.site_configuration import helpers

User = get_user_model()


class CatalogIntegration(ConfigurationModel):
"""
Expand Down Expand Up @@ -72,7 +72,4 @@ def get_internal_api_url(self):
return helpers.get_value('COURSE_CATALOG_API_URL', settings.COURSE_CATALOG_API_URL)

def get_service_user(self):
# NOTE: We load the user model here to avoid issues at startup time that result from the hacks
# in lms/startup.py.
User = get_user_model() # pylint: disable=invalid-name
return User.objects.get(username=self.service_username)

0 comments on commit d5aa834

Please sign in to comment.