Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom user model #2015

Merged
merged 2 commits into from
Feb 10, 2025
Merged

Add custom user model #2015

merged 2 commits into from
Feb 10, 2025

Conversation

rhysyngsun
Copy link
Contributor

@rhysyngsun rhysyngsun commented Feb 5, 2025

NOTE: #2013 should be reviewed first, as changes from it are included here.

What are the relevant tickets?

Part of https://github.com/mitodl/hq/issues/6679

Description (What does it do?)

This adds a custom user model and points the AUTH_USER_MODEL to it.
It updates the implementation of the 0001_initial.py model by having deleted this file and then recreating it after updating AUTH_USER_MODEL.

How can this be tested?

  • Ensure you have run the migrations from this PR before proceeding so that 0001_initial.py gets recorded as having been run. If it's been merged, you can just go ahead and run the migration off main.
  • Checkout this branch and test that ./manage.py showmigrations still lists 0001_initial.py under the users app as having been applied.
  • Run ./manage.py makemigrations --dry and verify there are no changes.
  • Start the app, verify the migrations run, and the app is functional.

When you are done with testing, be sure to run ./manage.py migrate users 0001 to revert not break things on other branches.

@rhysyngsun rhysyngsun force-pushed the nl/add_custom_user_model branch 2 times, most recently from 1b5170e to 914a11a Compare February 5, 2025 22:19
@rhysyngsun rhysyngsun changed the title Add customuser model Add custom user model Feb 6, 2025
@rhysyngsun rhysyngsun marked this pull request as ready for review February 6, 2025 20:45
@rhysyngsun rhysyngsun added the Needs Review An open Pull Request that is ready for review label Feb 6, 2025
@rhysyngsun rhysyngsun force-pushed the nl/add_custom_user_model branch from 914a11a to b870fda Compare February 6, 2025 22:34
@shanbady shanbady self-requested a review February 10, 2025 15:05
Copy link
Contributor

@shanbady shanbady left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting this error when trying to run show migrations

Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/django/apps/config.py", line 235, in get_model
    return self.models[model_name.lower()]
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
KeyError: 'user'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/django/contrib/auth/__init__.py", line 170, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/django/apps/registry.py", line 213, in get_model
    return app_config.get_model(model_name, require_ready=require_ready)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/django/apps/config.py", line 237, in get_model
    raise LookupError(
LookupError: App 'users' doesn't have a 'User' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/src/./manage.py", line 29, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/opt/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
  File "/opt/venv/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/opt/venv/lib/python3.12/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/opt/venv/lib/python3.12/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/src/profiles/models.py", line 49, in <module>
    User = get_user_model()
           ^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/django/contrib/auth/__init__.py", line 176, in get_user_model
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.User' that has not been installed

@rhysyngsun rhysyngsun force-pushed the nl/add_custom_user_model branch from b870fda to 2d912b4 Compare February 10, 2025 18:47
Copy link
Contributor

@shanbady shanbady left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shanbady shanbady added Waiting on author and removed Needs Review An open Pull Request that is ready for review labels Feb 10, 2025
@rhysyngsun rhysyngsun merged commit 62ba021 into main Feb 10, 2025
11 checks passed
@rhysyngsun rhysyngsun deleted the nl/add_custom_user_model branch February 10, 2025 19:56
@odlbot odlbot mentioned this pull request Feb 10, 2025
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants