0

I'm trying to create two related models in django, a custom user model and a company model.

where one user belongs to one company and one company has many users.

class Company(models.Model):

   name = models.CharField(max_length=255)

class User(AbstractBaseUser):

   objects = UserManager()

   id  = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, unique=True)
   name = models.CharField(max_length=67)
   username = models.CharField(max_length=255, unique=True)
   email = models.EmailField(max_length=255, unique=True)
   company = models.ForeignKey(Company, on_delete=models.CASCADE)

   last_login = models.DateTimeField(auto_now=True)
   date_joined = models.DateTimeField(auto_now_add=True)

   is_active = models.BooleanField(default=True)   
   is_staff = models.BooleanField(default=False)                 
   is_admin = models.BooleanField(default=False)
   is_supuser = models.BooleanField(default=False)


   USERNAME_FIELD = 'email'

   def __str__(self):
       return self.email

   def has_perm(self, perm, obj=None):
       return True

   def has_module_perms(self, app_label):
       return True

By default django generated two tables like this:

But I dont want any foreign keys directly on those tables, instead

i want a third auxiliary table with the two foreign keys from the models

like this:

enter image description here

How could I do something like that ?

Andrew
  • 8,322
  • 2
  • 47
  • 70
Pefori
  • 39
  • 5
  • 1
    You can use a many-to-many table with a "through" relation like this: https://stackoverflow.com/questions/37650362/understanding-manytomany-fields-in-django-with-a-through-model – Nik Sep 19 '22 at 13:19

0 Answers0