User authorization in django and the ways to achieve it if the default User model is extended to create custom User model

23 Views Asked by At

I am working on building a movie ticket booking website. I am told to implement authorization in the project so that only superuser can do certain actions on mongodb database(like adding movies and theatres). Please help me in understanding this process and the ways to achieve this. And also I am getting an error when creating superuser.
I am attaching the code for the reference.

  1. models.py -->
from .userManage import UserManager


class User(AbstractBaseUser):
    id=models.AutoField(primary_key=True,null=False)
    name=models.CharField(max_length=100)
    email=models.CharField(max_length=60)
    password=models.CharField(max_length=16)
    username=models.CharField(max_length=20,unique=True)
    mobileNumber=models.IntegerField()

    USERNAME_FIELD = 'username'
    objects=UserManager()

  1. usermanage.py - this python file is in same directory path
from django.contrib.auth.models import BaseUserManager
    

class UserManager(BaseUserManager):
    def create_user(self,username,password,**extra_fields):
        if not username: return ValueError("Username should be provided")
        user=self.model(username=username,**extra_fields)
        user.set_password(password)
        user.save()
        return user
    
    def create_superuser(self,username,password,**extra_fields):
        extra_fields.setdefault('is_staff',True)
        extra_fields.setdefault('is_superuser',True)
        return self.create_user(username,password,**extra_fields)

The error in creating superuser when I entered username and password: User() got unexpected keyword arguments: 'is_staff', 'is_superuser'.

Please help!

0

There are 0 best solutions below