Extending user model and relations

Hello,

I need to desing a little system where there are (user) clients who belong to Company and can make orders after logging in, as well as order (user) handlers (staff) and (user) sales representatives. Sales representatives are “attached” to company. Every company can have multiple sales person “attached”.

I decided to go with 3 kind of user profiles:

class User(AbstractUser):
   common_field1 = models.CharField()
   common_field2 = models.IntegerFeld()

class ClientUser(models.Model):
    user = models.OnteToOneField(User)
    additional_client_field = models.CharField()

class SalesUser(models.Model):
    user = models.OneToOneField(User)
    additional_salesuser_field = models.CharField()

class StaffUser(models.Model):
   user = models.OneToOneField(User)
   additional_staff_user_field = models.IntegerField()

Now question is - what is the best practice If I would like to assign SalesUsers.

  1. Should I make it in the SalesUser model via M2M field to Company
  2. Should I make it in the Company model via M2M field to SalesUser
  3. Should I make it in the User model via M2M field to Company
  4. Should I make it in the Company model via M2M relation to User model ?

Question is if I should use these profile extending models for key in relation from other places, like Order model where I store handler, seller, client ForeignKey

or maybe just point every relation etc. to User model and use the UserSeller etc. models for additional profile fetching ?

What is the best in your opinion approach ?

I will use use groups for permission managing which is another question if I should point groups on base User or User(Extension) level ?

Thanks

First, from a modelling perspective:

These two are functionally identical. If you choose to do this, it doesn’t materially matter which one you select.

Likewise, this also holds true for:

There’s no difference between these two.

Now, of the two sets, the answer is going to depend upon whether there’s any “meaning” to the concept of a Company being related to a User, if that user is not a SalesUser.

Note: You also need to define whether it’s appropriate for a User to be related to a combination of ClientUser, SalesUser, and StaffUser, or if they’re mutually-exclusive selections. That answer may affect how you choose to model this data.

This depends upon the answers to the previous questions. For example, if a User can be both a ClientUser and a SalesUser, how do you need to address the semantics of permissions in that situation?

A more precise answer may also depend upon the specifics of the additional_xxxx_field being referenced in your models. There are potentially multiple combinations of solutions affected by this.

Both SalesUser and StaffUser would be a member of Company model too. I will also create a Company mode which sells the product. So I can assume that in my Company models will be one company, for which there will be assigned StaffUser’s and SalesUser’s (and probably client users for ui testing pourposes etc.) and many Companies for which there will only be ClientUser’s attached. These will be just clients.

This depends upon the answers to the previous questions. For example, if a User can be both a ClientUser and a SalesUser , how do you need to address the semantics of permissions in that situation?

I would like StaffUser to be able to make purchases (be a Client) or a SalesPerson too and v-ce versa because in my Order model will be responsible for purchchases I will have to state who placed/created the order, who will handle it, and who is responsible for any further actions on company client side (for example accept the offer first, or even just to notify by email about the purchase)

For now I have users and simply some of them are allowed to login in to the django admin (via is_staff) attribute. You are probably referring to that some users may be different type of user depending on the role it’s having in the ordering process. I can only be sure that ClientUser will never be able to login the django admin, or become a SalesUser or StaffUser.

Main question is if in my Order class:

class Order(models.Model):
    company = models.ForeignKey(Company)
    placed_by_user = models.ForeignKey(User) # or maybe field should be named client.. with relation to User with limit_choices_to where user.client (onetoone) is available or ClientUser?
    handled_by_staff_user = models.ForeignKey(User) # StaffUser ?

Also my intention is to make system not too complicated (as much as It can be)
Thank you for the response, help and hoping the above will make more clear what my intentions are.