多对多查询的 Django 模型文件更新

Posted

技术标签:

【中文标题】多对多查询的 Django 模型文件更新【英文标题】:Django models file update for many to many query 【发布时间】:2021-04-12 11:45:12 【问题描述】:

所有, 这是我的困境。我创建了 Django models.py 文件,其中包含 2 个类,CustTeamMembers 和 CustTeamName。两者之间存在多对多关系,称为 cust_members。这意味着一个团队成员可以在多个团队中,并且多个团队可以拥有同一个成员。

我需要创建一种方法,为每个团队选择一名成员作为团队领导。该团队成员需要来自 cust_members 列表。

到目前为止,我还没有运气。任何帮助将不胜感激。

我的 models.py 文件:

from django.db import models

# This will hold the team members
class CustTeamMembers(models.Model):
    member_first_name = models.CharField(max_length = 100)
    member_last_name  = models.CharField(max_length = 100)
    member_email      = models.EmailField(unique=True)
    member_phone      = models.CharField(max_length = 25, blank=True)

    class Meta(object):
        ordering = ['member_last_name']

    def __str__(self):
        return self.member_first_name + ' ' + self.member_last_name

# This will hold the customer name
class CustTeamName(models.Model):
    cust_name    = models.CharField(max_length = 100)
    #cust_lead    = models.ForeignKey(CustTeamMembers, on_delete=models.CASCADE, blank=True)
    cust_members = models.ManyToManyField(CustTeamMembers, blank=True)
    cust_meet    = models.CharField(max_length = 40, blank=True)
    cust_status  = models.TextField(blank=True)

    def __str__(self):
        return self.cust_name

    def get_members(self):
        return ", ".join([str(p) for p in self.cust_members.all()]

~埃德

【问题讨论】:

【参考方案1】:

我会建议使用through 并自定义中间表。像这样。

from django.db import models

# This will hold the team members
class CustTeamMembers(models.Model):
    member_first_name = models.CharField(max_length = 100)
    member_last_name  = models.CharField(max_length = 100)
    member_email      = models.EmailField(unique=True)
    member_phone      = models.CharField(max_length = 25, blank=True)

    class Meta(object):
        ordering = ['member_last_name']

    def __str__(self):
        return self.member_first_name + ' ' + self.member_last_name

# This will hold the customer name
class CustTeamName(models.Model):
    cust_name    = models.CharField(max_length = 100)
    #cust_lead    = models.ForeignKey(CustTeamMembers, on_delete=models.CASCADE, blank=True)
    cust_members = models.ManyToManyField(CustTeamMembers, blank=True, through="TeamsAndMembers")
    cust_meet    = models.CharField(max_length = 40, blank=True)
    cust_status  = models.TextField(blank=True)

    def __str__(self):
        return self.cust_name

    def get_members(self):
        return ", ".join([str(p) for p in self.cust_members.all()]

class TeamsAndMembers(models.Model):
    team = models.ForeignKey(CustTeamName, on_delete=models.CASCADE)
    member = models.ForeignKey(CustTeamMembers, on_delete=models.CASCADE)
    is_leader = models.BooleanField(default=False)

【讨论】:

以上是关于多对多查询的 Django 模型文件更新的主要内容,如果未能解决你的问题,请参考以下文章

Django ORM - 通过模型查询多对多?

在 django 中获取具有多对多关系的复杂查询集

如何在双连接关系之后在 Django 中执行查询(或:如何绕过 Django 对多对多“通过”模型的限制?)

过滤后更新 Django 的多对多

查询 django 多对多

逻辑或 Django 多对多查询返回重复结果