4

I need create custom user. I'm using a user model with AbstractUser:

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.html import escape, mark_safe


class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)

settings.py:

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',

    'crispy_forms',

    'backend.classroom',
]

I believe that problem is in the following line:

AUTH_USER_MODEL = 'classroom.User'

error:

File "/home/davi/.local/share/virtualenvs/django-vue-template-Wl6a6m2J/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 82, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/home/davi/.local/share/virtualenvs/django-vue-template-Wl6a6m2J/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/davi/.local/share/virtualenvs/django-vue-template-Wl6a6m2J/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/home/davi/.local/share/virtualenvs/django-vue-template-Wl6a6m2J/lib/python3.6/site-packages/django/db/migrations/loader.py", line 226, in build_graph
    self.add_external_dependencies(key, migration)
  File "/home/davi/.local/share/virtualenvs/django-vue-template-Wl6a6m2J/lib/python3.6/site-packages/django/db/migrations/loader.py", line 191, in add_external_dependencies
    parent = self.check_key(parent, key[0])
  File "/home/davi/.local/share/virtualenvs/django-vue-template-Wl6a6m2J/lib/python3.6/site-packages/django/db/migrations/loader.py", line 173, in check_key
    raise ValueError("Dependency on app with no migrations: %s" % key[0])
ValueError: Dependency on app with no migrations: classroom

obs: The app classrom app is in the backend folder. I tried the following code too:

AUTH_USER_MODEL = 'backend.classroom.User'
Davi Luis
  • 396
  • 3
  • 16

2 Answers2

9

Try creating the initial migration for the classroom app before declaring it as AUTH_USER_MODEL, as anything that would ordinarily depend on auth.User now depends on classroom.User.

$ python manage.py makemigrations classroom
AKX
  • 152,115
  • 15
  • 115
  • 172
  • 3
    Quoting the Django docs: "Don’t forget to point AUTH_USER_MODEL to it. Do this _before_ creating any migrations or running `manage.py migrate` for the first time." It would be a mess to run migrations before setting the AUTH_USER_MODEL as that would create the User table with the wrong model. It wouldn't matter for the `makemigrations` command, but adding a custom User model in an existing app [isn't a straightforward undertaking](https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#changing-to-a-custom-user-model-mid-project). – dirkgroten Mar 05 '19 at 18:24
  • 2
    Good point well made, @dirkgroten. I actually have an answer relevant to that situation here, since I had to wrangle it with myself... https://stackoverflow.com/questions/47059198/lazy-reference-doesnt-provide-model-user/53460421#53460421 – AKX Mar 05 '19 at 18:42
  • I hope the OP is on a new project, not an existing one :-) but I hadn't seen the `migrations.SeparateDatabaseAndState` trick before, pretty cool. – dirkgroten Mar 05 '19 at 18:46
  • 1
    Checkout this https://stackoverflow.com/questions/25648393/how-to-move-a-model-between-two-django-apps-django-1-7 – E_K Mar 05 '19 at 19:05
0

Your error is on the app install i think? You are having classroom as app right? and the backend the project? if so then the correct configuration so the install app should be

INSTALLED_APPS = [
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
    django.contrib.humanize,
    crispy_forms
    classroom,
]

AUTH_USER_MODEL = classroom.User #classroom is app while User is the model
David Buck
  • 3,752
  • 35
  • 31
  • 35