【问题标题】:How to get django model by ManyToMany relationship?如何通过多对多关系获取 django 模型?
【发布时间】:2013-07-04 18:46:12
【问题描述】:

我得到了Player 实例(一个团队)的列表,例如team = [player1, player2, player3]

我如何通过一个查询“get_or_create”包含这些且只有这三个玩家的Team

这些是我的 django 模型:

class Player(models.Model):
    name = models.CharField(max_length=100)

class Team(models.Model):
    players = models.ManyToManyField(Player)

【问题讨论】:

标签: django model many-to-many


【解决方案1】:

文档说get_or_create 是一个快捷方式:

try:
    obj = Person.objects.get(first_name='John', last_name='Lennon')
except Person.DoesNotExist:
    obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
    obj.save()

所以我会根据您的用例进行如下调整:

from django.db.models import Q, Count
import operator

players = [player1, player2, player3]

team = Team.objects
    .filter(reduce(
         operator.and_,
         [Q(players=player) for player in players]
    ))
    .annotate(count=Count('players'))
    .filter(count=len(players))

# Note that this ^ returns a QuerySet. With your posted model there
# might exist several teams with exactly the same players.  

if not team.exists():
    team = Team.objects.create()
    team.players.add(*players)

稍微复杂一点,但工作流程是一样的:

  • 查询是否存在。
  • 如果不存在,则创建。

【讨论】:

  • 我接受了你的回答,但是我发现了一个小错误。 team.players.add() 不接受可迭代的但有多个参数,所以它应该是 team.players.add(*players) 使用 python 的 unpacking feature。看到它in django documentation
猜你喜欢
  • 1970-01-01
  • 2014-01-22
  • 2016-04-18
  • 2015-02-15
  • 2019-05-09
  • 2015-10-21
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多