3

I need to write a system that can have various types of user logged in, each with their own different set of data stored in the model. My first instinct was to use the new abstract base classes and make my own user types, so I have something like:

class BaseUser(AbstractBaseUser):
    email = models.EmailField(max_length=254, unique=True, db_index=True)

    USERNAME_FIELD = 'email'
    # ...

class StaffUser(BaseUser)
    name = ...
    # ...

class ClientUser(BaseUser)
    company_name = ...
    address = ...
    # ...

And some more user types. But they all need to log in through the same interface, and get directed to the right place, and only be able to do what their class of user should be able to, and only see what they should.

But it seems like I can't just use the regular authentication methods since it can't search multiple models (as far as I can tell). I came across this snippet (http://djangosnippets.org/snippets/2546/) which might be useful, but I'm not sure if this is really the best way to do it, or if I should just deal with having a lot of empty fields and just have one user class and use the permissions system. What is the cleanest way of doing this?

M.javid
  • 6,387
  • 3
  • 41
  • 56
Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
  • I would prefer multitable inheritance for this. there will be one table and one base model for User authentification. And there will be separate table for user_accounts extension linked to base table via OneToOne relationship. In this case You will be able to work with leaf classes if needed. – oleg Sep 23 '13 at 08:34
  • 1
    I would prefer one User model and OneToOne mappings to StaffData, ClientData etc models which you will create depending on a type field in your User model – OllyTheNinja Sep 14 '15 at 07:33

0 Answers0