0

I'm building a marketplace that allows users to sign up with a company or organization, add or invite other users to that company and manage those users. All users can create listings (products or service).

Currently I have an abstract user model and a company model with a foreign key to the user, which works well if a company only has one user, it looks like this:

class User(AbstractUser):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    phone_number = models.CharField(max_length=15)
    email = models.EmailField(unique=True)
    objects = UserManager()

    def __str__(self):
        return self.first_name


class Company(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    company_name = models.CharField(max_length=100, unique=True)
    company_address = models.CharField(max_length=100, unique=True)
    company_website = models.URLField()
    company_verified = models.BooleanField(default=False)

    def __str__(self):
        return self.company_name

What would be the best way to accomplish this, without giving the users permissions to the admin site?

I've looked at using django-organizations, but the documentation seems poor and they don't support postgresql.

S.Hefer
  • 91
  • 1
  • 2
  • 9
  • You may need a `Company_User` table to achieve a many-to-many relationship (or one-to-many). Another question asking for one-to-many relationship in Django https://stackoverflow.com/questions/6928692/how-to-express-a-one-to-many-relationship-in-django – Joseph Nov 22 '19 at 01:54
  • That's currently what the User model is doing already. Hence the foreign key on the Company model. I'm trying to figure out how to write the views and permissions for the business logic I'm trying to implement. – S.Hefer Nov 22 '19 at 02:41

0 Answers0