0

I am new to django and from a tutorial video, in the model.py file for the profile app, he created an instance of the User model in a one-to-one relationship with the profile model class like so:

`

from django.contrib.auth.models import User

class Profile(models.Model):
      user = models.OneToOneField(User, blank=True, null=True)

I understand that he created a one-to-one relationship with the profile model and the User model by creating an instance of the User model in the class.

I am trying to achieve this with AbstractUser as well.

I tried to do the same thing like so:

from django.contrib.auth.models import AbstractUser

class Blogger(models.Model):
      user = models.OneToOneField(AbstractUser, blank=True, null=True)

In the settings.py, I have connected the PostGresSql database like so:

DATABASES = {

     'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'bookandreviews',
        'USER': 'postgres',
        'HOST': 'localhost',
        'PASSWORD': '2030',
        'PORT': '5432'
    }
}

I ran py manage.py runserver and I got this error:

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
users.Bloggers.user: (fields.E300) Field defines a relation with model 'AbstractUser', which is either not installed, or is abstract.
users.Bloggers.user: (fields.E307) The field users.Bloggers.user was declared 
with a lazy reference to 'auth.abstractuser', but app 'auth' doesn't provide model 'abstractuser'.

I don't understand it. So I tried to add an auth_user_model, like so:

AUTH_USER_MODEL = 'users.Bloggers'

And I immediately got this issue:

AttributeError: type object 'Bloggers' has no attribute 'REQUIRED_FIELDS'

** Please can anyone help me understand all of this and also, is it possible to instantiate the AbstractUser model?**

theocode
  • 69
  • 8

1 Answers1

1

I think I have figured it out. AbstractUser is a subclass of the User model and it inherits from it. It should be used if you want to include some additional fields to the already laid out User model.

The best thing that can work here would be to use AbstractBaseUser, which only includes the authentication and can allow full control over the User model. You have to specify the username field (if you plan on changing the authentication from username to email) and the required fields though.

You can read this article to understand https://medium.com/@engr.tanveersultan53/abstractuser-vs-abstractbaseuser-in-django-7f231a276988. Or this question:

https://stackoverflow.com/questions/21514354/difference-between-abstractuser-and-abstractbaseuser-in-django#:~:text=AbstractUser%20is%20a%20full%20User,supply%20them%20when%20you%20subclass.

theocode
  • 69
  • 8