0

I have a couple of models like:

class Organization(models.Model):
   id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
   ....

class Customer(models.Model):
       id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
       ....

class Supplier(models.Model):
   id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
   ....

Each model have their own one or more users, I created a user model like this:

class User(AbstractBaseUser, PermissionsMixin):
  id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  email_address = models.EmailField(max_length=255, unique=True)
  name = models.CharField(max_length=255)

My question is how can I create a relationship between User and Organization, Customer, Supplier...? I found one solution like:

class User(AbstractBaseUser, PermissionsMixin):
  id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  email_address = models.EmailField(max_length=255, unique=True)
  name = models.CharField(max_length=255)
  organization = models.ForeignKey(Organization)
  customer = models.ForeignKey(Organization)
  supplier = models.ForeignKey(Supplier)

Which I did not like to do because I have several models that have their users. Another way I found is GenericForeignKey Relationship but also not a very good solution. One of the reasons for this is I have more models to implement similar relationships which becomes very complicated. Can anyone suggest an elegant solution for this? Thanks in advance!

bhattraideb
  • 407
  • 7
  • 25
  • Remove `organization`, `customer` and `supplier` fields from `User` model and add `user = models.ForeignKey(User, on_delete=models.CASCADE)` field to `Organization`, `Customer` and `Supplier` models – Ahtisham Jan 15 '22 at 15:45
  • @Ahtisham Will this allow me to create multiple users for Organization, Customer, and Supplier models? – bhattraideb Jan 17 '22 at 06:36

2 Answers2

0

Its not the best way to do it in user, Add the user as foreign key from the Models instead like this ,

class Organization(models.Model):
   id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
   user = models.ForeignKey(User, on_delete=models.CASCADE)
   ...

If you have multiple user for same organization create a separate table to handle relationship or use manytomany relations.

  • Will this allow me to create multiple users for Organization, Customer, and Supplier models? – bhattraideb Jan 17 '22 at 06:37
  • Take a look at the lines below the sample code of my answer. That way you can. – Uzzal H. Mohammad Jan 17 '22 at 07:00
  • I thought this will be only the solution according to https://stackoverflow.com/questions/7573590/can-a-foreign-key-be-null-and-or-duplicate#:~:text=Short%20answer%3A%20Yes%2C%20it%20can,table%20(the%20parent%20table). – bhattraideb Jan 21 '22 at 09:48
0

I think case you should use

class Organization(models.Model):
   id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
   user = models.ForeignKey(User, on_delete=models.SET_NULL)

models.SET_NULL is more suitable here than models.CASCADE beacause if the organisation is deleted User should not be deleted too.