0

I want to use an extra field let me distinguish between user roles.

For that, I've overriden the User class:

class User(AbstractUser):
    role = models.CharField(max_length=21, blank=False)

I've also put this in settings.py:

AUTH_USER_MODEL = 'shopping.User'

The problem is that I have the new field in my database, but, if I want to create a new user using the admin, it doesn't ask me for the role. I suppose that I have to override the save method too, but I don't know how.

Thanks in advance

student-n
  • 3
  • 2
  • https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms – lucutzu33 Jun 24 '21 at 17:45
  • I don't know if I can use it because I am using DRF. I receive the fields from the backend by an http post. I want to take the role from the http.post and add it to the user – student-n Jun 24 '21 at 17:51
  • Does this answer your question? [Customizing an Admin form in Django while also using autodiscover](https://stackoverflow.com/questions/471550/customizing-an-admin-form-in-django-while-also-using-autodiscover) – Jimmar Jun 24 '21 at 19:34

1 Answers1

0

I would recommend you to rewrite used Model-Like this and it will much easier and you can add filed like you want

from django.contrib.auth.models import (
        AbstractBaseUser,
        BaseUserManager,
        PermissionsMixin,
    )


#You can set here required field for register new user.
class UserManager(BaseUserManager):
    def create_user(self, email, username, password=None, **extra_fields):
        if not email:
            raise ValueError("Users must have an email address")
        if not username:
            raise ValueError("Users must have a username")
        user = self.model(
            email=self.normalize_email(email), username=username.lower(), **extra_fields
        )
        user.set_password(password)
        user.save(using=self._db)

        return user

    def create_superuser(self, email, username, password):
        user = self.create_user(email, username, password)
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)

        return user

class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=30, unique=True)
    email = models.EmailField(max_length=255, unique=True)
    fullname = models.CharField(max_length=60, blank=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    date_joined = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    role = models.CharField(max_length=21, blank=False)

    objects = UserManager()

    USERNAME_FIELD = "username"  
    REQUIRED_FIELDS = ["email"]   #Requiref field set here


 AUTH_USER_MODEL = "shopping.User"
Rahul Raj
  • 516
  • 7
  • 15
  • i've tried but it saves all but the role when, in Angular I make this http request: ` signup(username:string, email:string, password1:string, password2:string){ //todo var role:string = "Administrador" return this.http.post( this.apiRoot.concat('signup/'), {username, email, password1, password2, role} ).pipe( tap(response=>this.setSession(response)), shareReplay() ); } ` to one of this view: ` path('auth/', include('rest_auth.urls')), path('auth/signup/', include('rest_auth.registration.urls')), ` sorry for the format – student-n Jul 01 '21 at 17:57