简体   繁体   English

多对多或一对多Django

[英]Many to Many or One to Many Django

I have the following two models in Django. 我在Django中有以下两个模型。 One is basically an extension of the base Django user class and the other is a company model. 一个基本上是基本Django用户类的扩展,另一个是公司模型。 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)

First, is there a reason to extend the User model? 首先,是否有扩展User模型的理由? The default model already includes a first_name and last_name field, so you don't need an additional model just for that data. 默认模型已经包含first_namelast_name字段,因此您不需要仅用于该数据的其他模型。 Similarly, you don't really need CompanyContact because the User model also contains email and name (again, through first_name and last_name ) fields. 同样,您实际上并不需要CompanyContact因为User模型还包含emailname (同样通过first_namelast_name )字段。

You can add in your contacts as a ManyToManyField . 您可以将联系人添加为ManyToManyField If you want to use the custom Profile model instead of User , just replace User (in the ManyToManyField ) with Profile . 如果要使用自定义Profile模型而非User ,只需将User (在ManyToManyField )替换为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: 因此,您可能具有类似于以下内容的CompanyContact模型:

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: 然后,声明ManyToManyField关系以使用此新模型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM