extending a custom user (AbstractBaseUser) and displaying in admin

40 views
Skip to first unread message

Elic

unread,
Nov 23, 2019, 12:26:37 AM11/23/19
to Django users
Hi,

Hope you are all doing well.

I had to change the User object to support several use-cases in my system.

I have created a custom User object that extends AbstractBaseUser.This object only has email, password, last_login and the permissions (is_stuff, is_superuser). I also created UserManager for it.

If I go to the /admin I can create new users and it works well.

However, I have to extends this user, to allow different kind of users:

1. Company 
2. Employee

In both cases I hold a onetoone User object reference:

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='+')

I read documentation and found places that describe how to add inline nesting in the admin UI, however, in all the scenarios I saw it shows that the inline is done on the User object which then adds part for an Employee.

However, this is not what I want. I would like to enable creating a user via /admin only when creating a Company or Employee.

For Example when I want to create an Employee I will have the following fields:
- email
- password
(these 2 come from the User)

- name
- birthday
(these are coming from the Employee)

Is it possible to achieve?

Thank you,
Elic

Underground Creatorz

unread,
Nov 24, 2019, 3:34:37 AM11/24/19
to django...@googlegroups.com
Hello all. Have a merry christmas and happy new Year. (moorest...@gmail.com)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc0222b2-c46d-4d59-9028-f8057d7ad9c6%40googlegroups.com.

Motaz Hejaze

unread,
Nov 24, 2019, 9:16:16 AM11/24/19
to django...@googlegroups.com
One way around is to make the models Employee and Company to inheret from User in models.py


class Employee(User):
 ...

class Company(User):
 ...




Elico

unread,
Nov 24, 2019, 11:53:03 AM11/24/19
to django...@googlegroups.com
Hi Motaz,

Thank you for your response. 

I didn’t try that yet, however my assumption is that I would have duplicates of fields inherited from the User class. 



Reply all
Reply to author
Forward
0 new messages