-1

I'm trying to do a logistics web application in Django and I need to assign different roles to the users.

I have normal users (clients), employees (office employees, delivery man, truck drivers...) and supervisors.

I need to do the following:

  • Office employees and delivery man users: need to have an office assigned
  • Truck drivers users: need to have an assigned route

I've made 2 models (Offices and Routes) but I don't know how to relate the users with these models.

I've read websites that create custom classes inherited from AbstractBaseUser or AbstractUser, but I don't know how to do it with this special case.

I hope to hear the right answer here. Thanks in advance!

Edit: my question is different as this other, as I need to handle with different types of User, not just one.

Luis
  • 1
  • 2
  • Possible duplicate of [Extending the User model with custom fields in Django](https://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django) – Saeed Zhiany Oct 21 '19 at 07:47

1 Answers1

1

You can get help from https://realpython.com/modeling-polymorphism-django-python/ and maybe the best one is Sparse Model for you

mtf
  • 329
  • 2
  • 13
  • Thanks! I've seen that link and [this other](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model). According to these links, I understand that I have to extend User Model and add all the model fields of both types of users, and raise exceptions to overcome with integrity problems. Is it correct? – Luis Oct 27 '19 at 18:29
  • Also, how could I associate the choice list with a group? For example, if user_type = A, associate with group A – Luis Oct 27 '19 at 18:41
  • @Luis You can create a manager for each of them to filter base on the type – mtf Oct 28 '19 at 11:45