Question: Many to Many or One to Many Django

Question

Many to Many or One to Many Django

Answers 1
Added at 2016-12-31 20:12
Tags
Question

I have the following two models in Django. One is basically an extension of the base Django user class and the other is a company model. I want to say that a user can belong to one or more companies and that a company can also have one or more contacts = "Users". Would this be a correct setup? How should I represent the tie between user and company?

User Profile model:

class Profile(models.Model):
    user = models.OneToOneField(User)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

Company model:

class Company(models.Model):
    name = models.CharField(max_length=120)
    account_name = models.CharField(max_length=10, default="")
    sales_rep = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_sales", default="")
    csr = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_csr", default="")

class CompanyContact(models.Model):
    name = models.CharField(max_length=40, default="")
    email = models.CharField(max_length=50, default="")
    user = models.ForeignKey(User)
    company = models.ForeignKey(Company)
Answers
nr: #1 dodano: 2016-12-31 20:12

First, is there a reason to extend the User model? The default model already includes a first_name and last_name field, so you don't need an additional model just for that data. Similarly, you don't really need CompanyContact because the User model also contains email and name (again, through first_name and last_name) fields.

You can add in your contacts as a ManyToManyField. If you want to use the custom Profile model instead of User, just replace User (in the ManyToManyField) with Profile.

class Company(models.Model):
    name = models.CharField(max_length=120)
    account_name = models.CharField(max_length=10, default="")
    sales_rep = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_sales", default="")
    csr = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_csr", default="")
    contacts = models.ManyToManyField(User) # or Profile

This allows each company to have many contacts and each user to be a contact of many companies – thus many-to-many.


Now, if you wanted extra data to describe the many-to-many relationship, you can have another model for that. For example, you may want to keep a record if the contact is still active or what their role is. So, you may have a CompanyContact model that is similar to:

class CompanyContact(models.Model):
    active = models.BooleanField(default=False)
    role = models.CharField(max_length=50, default="")
    user = models.ForeignKey(User) # or Profile
    company = models.ForeignKey(Company)

Then, declare the ManyToManyField relationship to use this new model:

class Company(models.Model):
    ...
    contacts = models.ManyToManyField(User, through="CompanyContact")
    # or contacts = models.ManyToManyField(Profile, through="CompanyContact")
Source Show
◀ Wstecz