In Django 5.1 difference between Model and AbstractUser
I am looking at the Django Documentation and am a bit confused as the differences between:
from django.contrib.auth.models import AbstractUser
from django.db.models import Model
I tried doing the following in a class and got an error that has to do with the following topic: Python multiple inheritance and MRO
The error emerged because I had done the following:
class Employee(Model, AbstractUser):
pass
When I went to make migrations in Django, an error message said it violated MRO. When I searched what that meant on Google, I found a Stack Overflow post that mentions that it happens when it can't decide between the two classes, a specific value, or a method.
In my research, there might be a conflict amongst similar methods in class.
I am expecting to have an Employee that has a custom authentication, and is an entity in the database. How would I go about achieving this, do I have to pick AbstractUser
over Model
over using them both?
AbstractUser
is already inheriting from Model
, so you do not have to pick between them - using AbstractUser
by itself gets you both of them.
So do this:
class Employee(AbstractUser):
pass
Here's the inheritance chain for AbstractUser
:
# django.contrib.auth.models
class AbstractUser(AbstractBaseUser, PermissionsMixin):
...
# django.contrib.auth.base_user
class AbstractBaseUser(models.Model):
...