When applying the AbstractUser changes to the User class (in models.py and admin.py) I was encountering an error when running the python manage.py createsuperuser command.
I would love any feedback as to why this error was appearing (I’m using sql server 2016 and also had a User table previously made).
Hopefully someone can fill me in as to why this was happening. It certainly makes sense as to why this solution works, but as to why Mosh’s demonstration was working, I couldn’t find under the hood what was causing mine to break.
Either way , I hope this helps someone in the future, trying to follow this video with an existing app.
Are you saying that you started your project using Django’s built-in User model and you are trying to swap it out with your custom user model? If so, this ain’t straightforward because Django already set up a bunch of things assuming you are using the built-in user model. So, you have to change your code as well as the database schema.
Things are better explained here and here
Also, Mosh does go through this in part 2 user auth chapter.
No, it was my own custom user model. I was able to fix the problem, but unfortunately I don’t understand why.
In the admin.py file of the app where my user table lives, I changed:
@admin.register(UserAdmin)
class UserAdmin(BaseUserAdmin):
To:
class UserAdmin(BaseUserAdmin):
code
code
admin.site.register(User, UserAdmin)
I originally tried @admin.resgister(User, UserAdmin) with no luck. Again, I am unsure as to why the second option worked for me, but figured I would update my fix because it is more inline with the course and a better solution than the linked option that I posted previously.
Again, I’d like to state that I am going thru this video and implementing some of Mosh’s techniques into my own project, and not following along with his project. Hopefully this can help someone get un-stuck in the future.
Oh, I see what you mean now. But without looking at your code, this is as far as I can tell.
Django’s createsuperuser command calls the create_superuser() method from your UserManager. The default UserManager requires you to pass the username to create a superuser.
The problem you are having is that the user data collected from createsuperuser command didn’t include the field called username. Chances are you are not requiring username.
Now, why did this happen? this is the part that tricks me. Normally, if your custom user model extends AbstractUser, you are guaranteed to have a username field. So, there must be some other things that you are doing, which I can’t tell without looking at the code.
The solution you posted works because it has every necessary bit. The user extends AbstractBaseUser, specified USERNAME_FIELD, and implemented a custom user manager.