在Django中,如何在没有显式查询的情况下从多对多关系中的额外字段中检索数据?

时间:2022-10-04 16:35:09

Given a situation in Django 1.0 where you have extra data on a Many-to-Many relationship:

鉴于Django 1.0中的情况,你有多对多关系的额外数据:

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

class Team(models.Model):
  name = models.CharField(max_length=40)
  players = models.ManyToManyField(Player, through='TeamPlayer', related_name='teams')

class TeamPlayer(models.Model):
  player = models.ForeignKey(Player)
  team = models.ForeignKey(Team)
  captain = models.BooleanField()

The many-to-many relationship allows you to access the related data using attributes (the "players" attribute on the Team object or using the "teams" attribute on the Player object by way of its related name). When one of the objects is placed into a context for a template (e.g. a Team placed into a Context for rendering a template that generates the Team's roster), the related objects can be accessed (i.e. the players on the teams), but how can the extra data (e.g. 'captain') be accessed along with the related objects from the object in context (e.g.the Team) without adding additional data into the context?

多对多关系允许您使用属性(Team对象上的“players”属性或通过其相关名称使用Player对象上的“teams”属性)访问相关数据。当其中一个对象被放置到模板的上下文中时(例如,一个团队放置在一个Context中,用于渲染一个生成Team的名单的模板),可以访问相关对象(即团队中的玩家),但是怎么能额外数据(例如“队长”)可以与上下文中对象(例如团队)中的相关对象一起访问,而无需在上下文中添加其他数据?

I know it is possible to query directly against the intermediary table to get the extra data. For example:

我知道可以直接查询中间表来获取额外的数据。例如:

TeamPlayer.objects.get(player=790, team=168).captain

Or:

要么:

for x in TeamPlayer.objects.filter(team=168):
  if x.captain:
    print "%s (Captain)" % (x.player.name)
  else:
    print x.player.name

Doing this directly on the intermediary table, however requires me to place additional data in a template's context (the result of the query on TeamPlayer) which I am trying to avoid if such a thing is possible.

然而,直接在中间表上执行此操作需要我在模板的上下文(TeamPlayer上的查询结果)中放置其他数据,如果可能的话,我试图避免这种情况。

1 个解决方案

#1


9  

So, 15 minutes after asking the question, and I found my own answer.

所以,在提问后15分钟,我找到了自己的答案。

Using dir(Team), I can see another generated attribute named teamplayer_set (it also exists on Player).

使用dir(Team),我可以看到另一个名为teamplayer_set的生成属性(它也存在于Player中)。

t = Team.objects.get(pk=168)
for x in t.teamplayer_set.all():
  if x.captain:
    print "%s (Captain)" % (x.player.name)
  else:
    print x.player.name

Not sure how I would customize that generated related_name, but at least I know I can get to the data from the template without adding additional query results into the context.

不知道如何自定义生成的related_name,但至少我知道我可以从模板中获取数据,而无需在上下文中添加其他查询结果。

#1


9  

So, 15 minutes after asking the question, and I found my own answer.

所以,在提问后15分钟,我找到了自己的答案。

Using dir(Team), I can see another generated attribute named teamplayer_set (it also exists on Player).

使用dir(Team),我可以看到另一个名为teamplayer_set的生成属性(它也存在于Player中)。

t = Team.objects.get(pk=168)
for x in t.teamplayer_set.all():
  if x.captain:
    print "%s (Captain)" % (x.player.name)
  else:
    print x.player.name

Not sure how I would customize that generated related_name, but at least I know I can get to the data from the template without adding additional query results into the context.

不知道如何自定义生成的related_name,但至少我知道我可以从模板中获取数据,而无需在上下文中添加其他查询结果。