0

I am building a web application that will enable two different types of users to log in, with each user group having access to different pages on the site.

As the information collected is similar for each type of user, I am planning to use only one custom user model with two boolean fields for each user type. The model will be defined as follows:

class My_Users(AbstractBaseUser):
...
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_typeA = models.BooleanField(default=False)
is_typeB = models.BooleanField(default=False)

...

I intend to use OneToOne fields based on user IDs in other models, for both typeA and typeB users. So for example, there could be:

class Model1(models.Model):
    id = models.OneToOneField(TypeA, on_delete=models.CASCADE,  primary_key=True,)
    ...

class Model2(models.Model):
    id = models.OneToOneField(TypeB, on_delete=models.CASCADE,  primary_key=True,)
    ...

My question are the following:

1/ This will imply that some ids created in my user model will be typeA and some others will be typeB. Will this be a problem for the one to one reliationship? i.e. will it be a problem that the ids are not incremental (type A could be id 1, 3, 4, 5, etc... whilst type B could have ids 2, 6, 7, etc...) for the one-to-one relationship.

2/ Is this the best set up in order to ensure that the app is scalable. If not, what would this be?

Thanks.

RobinW2
  • 99
  • 1
  • 7

1 Answers1

0
  1. No, it's not a problem for the one-to-one relationship. As stated in this SO answer, a OneToOne is just a ForeignKey with unique=True and the reverse relation being singular.

  2. Would be helpful to a little more detail on how you're dividing up the app based on the type of user, and what you mean by "scalable". Django's Groups and permissions is a more generalisable solution for "each user group having access to different pages on the site", and then you can check group membership when you're creating objects too.

Community
  • 1
  • 1
neomanic
  • 346
  • 1
  • 7
  • Hi neomanic, thanks for your answer. By "scalable" I am referring to the size of the user base on the application, i.e. when it grows to a significant amount of users. Regarding the two types of users, they will have access to different type of functionalities. TypeA will be able to add and manage objects on the site while TypeB will be able to buy and review. – RobinW2 Feb 15 '17 at 23:38
  • So you have regular users and administrators? That's exactly where I use the groups/permissions functionality in my webapp. It would be simple enough later on to drop down to a more basic implementation based solely on User attributes if scalability starts to suffer, but I wouldn't worry about that in the initial design. – neomanic Feb 16 '17 at 00:05
  • Great thanks for your help. I'll look a bit more into Groups and permissions and note your answer as correct. – RobinW2 Feb 16 '17 at 23:31