#Looking for advices on CustomUserModel

25 messages · Page 1 of 1 (latest)

summer gull
#

Hi, I'm trying to set a custom user model for my app where there will be 4 possible roles : admin, company owner, company employee, clients.
I wrote couple of things but I would like to know, how I should approach this so when an owner is created a company is created as well. Lastly when the company is created that the only the owner and admin can add an employee to a company. See below snippets of my admin and models.py

models.py

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=125)
    address = models.CharField(max_length=125)
    description = models.TextField(blank=True)
    telephone = models.IntegerField(blank=False)
    
    def __str__(self):
        return self.name

def create_company(name):
    if not name:
        raise ValueError('Company must have a name')
    company = Company.objects.create(name = name)
    
    return company

class UserManager(BaseUserManager):  
    def create_owner(self, email, first_name, last_name, name, password):
        company = create_company(name = name)
        user = self.create_user(
            email = self.normalized_email(email),
            first_name = first_name,
            last_name = last_name,
            company = company,
            password = password,
        )
        user.save(using=self._db)
        return user

admin.py

    name = forms.CharField(label="name")
    owner = forms.CharField(label="Company owner")
    
    class Meta:
        model = Company
        fields = ["name","address","description","telephone"]

    def save(self, commit=True):
        user = super().save(commit=False)
        if commit:
            user.save()
        return user

class UserAdmin(BaseUserAdmin):
    role = ROLE_CHOICES = ((1, 'Create Company'),
        (2, 'Create User'))

    if role == 2:
        add_form1 = UserCreationForm
    else:
        add_form2 = CompanyCreationForm
drowsy badge
#

I'm not seeing custom user model here?

#

and why Company form returns company named user ?

#

Feels like you are confusing some things here

summer gull
#

you're right, this should be this part ```
class User(AbstractBaseUser,PermissionsMixin):
ADMIN = 1
VENDOR_OWNER = 2
VENDOR_EMPLOYEE = 3
CLIENT = 4

ROLE_CHOICE = (
    (ADMIN, "Admin"),
    (VENDOR_OWNER, "V_Owner"),
    (VENDOR_EMPLOYEE, "V_Employee"),
    (CLIENT, "Client"),
)

id = models.AutoField(primary_key=True)
role_picker = models.SmallIntegerField(choices=ROLE_CHOICE,blank=True,null=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
username = models.CharField(max_length=50,unique=True)
email = models.EmailField(max_length=50,unique=True)
phone_number = models.CharField(max_length=12,blank=True)
 
# REQUIRED

date_joined = models.DateTimeField(editable=True)
last_login = models.DateTimeField(auto_now=True)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_superadmin = models.BooleanField(default=False)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS =['username','first_name','last_name','date_joined']

objects = UserManager()

def __str__(self):
    return self.email

def has_perm(self,perm,obj=None):
    return self.is_admin

def has_module_perms(self, app_label):
    return True
```
#

for the company form error from my side as I use similar structure as my User form I forgot to rectify it

drowsy badge
#

uhum, I see, just a note - why didn't you use AbstractUser ? You seem to add most of it to base user

summer gull
#

from what I read that AbstractBaseUser is more flexible for custom fields

drowsy badge
#

Important thing I don't see is how user is realated to Company?

summer gull
#

I put it here ```class UserManager(BaseUserManager):
def create_owner(self, email, first_name, last_name, name, password):
company = create_company(name = name)

#

ah I think confused so things

drowsy badge
#

Base user is good when you don't need some of fields, adding and overriding is just fine on AbstractUsr

#

but that's not important

drowsy badge
#

and by your description you really want it

#

regarding:

how I should approach this so when an owner is created a company is created as well
Sounds like it should be solved by form, since you probably want a company name input to create Company?
But technically you can do it by overriding User save() method and checking if set as owner and doesn't yet have company

#

Anpther side consideration: you've set role type as strict choice, it works fine most of the time, but have you considered: Admin being a Client, empoyee being a client to same company, owner being a client to another company, etc ?

summer gull
summer gull
#

Regarding the roles, for Admin I had in mind that it would be use only for website management and that if an admin want to be a client he would need to create a client account. For Employee and Company owner I had in mind that the person only manage the company related data and that if it's want to be client he would have to create an account for it

#

I'm aware it probably doesn't include all the possible cases but I wanted to get on with strict choices to not get lost as I'm still learning by reading/ doing

drowsy badge
drowsy badge
#

And to last part:

Lastly when the company is created that the only the owner and admin can add an employee to a company. See below snippets of my admin and models.py
That's basically object-level permissions. Django doesn't have those built in
For your case I'd just build a custom management system that would filter company by user.company