2

So I am working on implementing a custom user model, and to do so i followed tutorial 'this tutorial.

These are the models:

class UserManager(BaseUserManager): """Define a model manager for User model with no username field.""" use_in_migrations = True def _create_user(self, email, password, **extra_fields): """Create and save a User with the given email and password.""" if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=None, **extra_fields): """Create and save a regular User with the given email and password.""" extra_fields.setdefault('is_staff', False) extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): """Create and save a SuperUser with the given email and password.""" extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_staff') is not True: raise ValueError('Superuser must have is_staff=True.') if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(email, password, **extra_fields) class User(AbstractUser): """User model.""" username = None email = EmailField(_('email address'), unique=True) USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] objects = UserManager() 

I am now trying to run python3 manage.py migrate and I get this error:

[wtreston] ~/gdrive/mysite2/ $ python3 manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, reviews, sessions, users Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line utility.execute() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 164, in handle pre_migrate_apps = pre_migrate_state.apps File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/state.py", line 218, in apps return StateApps(self.real_apps, self.models) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/state.py", line 295, in __init__ raise ValueError("\n".join(error.msg for error in errors)) ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. The field reviews.Answer.student was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. The field reviews.ReviewBlock.teacher was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. The field users.Student.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. The field users.Teacher.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. 

In my settings.py I have this line AUTH_USER_MODEL = 'users.User' . and when I reference the AUTH_USER_MODEL with ForeignKeys, I first have from mysite import settings and then have ForeignKey(settings.AUTH_USER_MODEL)

Any help would be helpful! Thanks

10
  • users.User has abstract = True? Commented Dec 28, 2017 at 15:08
  • @schwobaseggl added the two models I am using as I am not sure what you mean? Commented Dec 28, 2017 at 15:11
  • what does your migration file look like? Commented Dec 28, 2017 at 15:12
  • You should generally import settings from django.conf which will provide all the settings in your DJANGO_SETTINGS_MODULE and all the necessary default values. Also, try using django's get_user_model() function instead of settings.AUTH_USER_MODEL Commented Dec 28, 2017 at 15:18
  • @schwobaseggl Ok. I have made all the changes you suggested, but I am still getting the same error. Commented Dec 28, 2017 at 15:34

1 Answer 1

1

You have inherited AbstractUser for your custom user model and you are inheriting BaseUserManager for UserManager modify your manager like below by inheriting UserManager:

class MyUserManager(UserManager): def create_user(self, email, password=None, **kwargs): # override this method def create_superuser(self, email, password, **kwargs): # override this method 

clean up migrations and database first and then apply migration with this changes.

Sign up to request clarification or add additional context in comments.

2 Comments

what do you mean by clean up migrations and database first and then apply migration with this changes. I can delete the most recent migration, however I dont know what you mean by clean up the database
cleaning migrations won't delete any existing created table in database. for simply you can drop all tables of database.. like fresh start

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.