Django中的许多关系 [英] Many to Many relationships in Django

查看:148
本文介绍了Django中的许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django的某些型号的设计工作,并希望得到一些建议。我有一个团队模型,其中许多用户可以参与其中。用户也可以是许多团队的成员,但他们不能是同一个团队的成员两次。我还要跟踪每个团队/用户组合的单独信息。除此之外,还有一个用户是每个团队的管理员,但每个团队可能只有一个管理员。我有一些精简数据模型定义如下:

  class Team(models.Model):
name = models。 CharField()
members = models.ManyToManyField(User,through ='Membership')
admin = models.ForeignKey(User)

class成员资格(models.Model):
user = models.ForeignKey(User)
team = models.ForeignKey(Team)
extra_data = models.CharField()

所以我想我的两个问题是:



1)我如何使它成为会员模式,没有用户和团队组合出现不止一次?



2)有没有更好的方式来表示团队中的管理员,同时确保每个团队只有一个一?看来,如果我正在存储这样的管理员,而执行只有一个管理员的规则,查询所有团队成员变得太麻烦,因为管理员不会在会员资格表中。如果它们存储在会员资格中,那么我不得不执行另一个查询来查看他们是否是该团队的管理员。在会员资格中使用is_admin字段的想法弹出我的头脑,但我不确定如何保持每个团队1个管理员的限制。



任何帮助将是伟大的赞赏。



更新:
看起来像unique_together元标签是我正在寻找的第一个问题。

解决方案

第一个问题:



您可以添加约束来阻止它:

 类成员资格(models.Model):
user = models.ForeignKey(User)
team = models.ForeignKey(Team)
extra_data = models.CharField()
class Meta:
unique_together =(('user ','team'),)

第二个问题:



您可以向会员模型添加排名字段,并将'user'和'rank'unique_together,正整数字段。而具有 rank == 1 的用户是管理员。或类似它将执行您的方案,但编码维护计划可能很麻烦。你可能会比以前更好。


I am working on a design for some models in Django and wanted to get some advice. I have a model for teams, of which many users can be a part of. The users can also be members of many teams, but they cannot be members of the same team twice. There is also separate information I want to track for each team/user combo. On top of that, there will be a user who is an "admin" for each team, but each team may have only a single admin. I have some condensed data model definitions as follows:

class Team(models.Model):
    name = models.CharField()
    members = models.ManyToManyField(User, through='Membership')
    admin = models.ForeignKey(User)

class Membership(models.Model):
    user = models.ForeignKey(User)
    team = models.ForeignKey(Team)
    extra_data = models.CharField()

So I guess my two questions are:

1) how would I make it so that on the Membership model, no user and team combination appeared more than once?

2) Is there a better way to signify an Admin on the team, while making sure that each team only had a single one? It seems like if I'm storing the admin like this, while enforcing the rule of having only a single admin, it becomes too cumbersome to query all team members since the admin will not be in the Membership table. And if they are stored in membership, than I'd have to perform another query to see if they are admin of this team or not. The thought of using an "is_admin" field in Membership popped in my head, but I'm unsure of how to keep the constraints of 1 admin per team.

Any help would be greaty appreciated.

UPDATE: looks like the unique_together meta tag is what I'm looking for on the first question. I'm still curious about the second one though....

解决方案

First question:

You can add a constraint to prevent it:

class Membership(models.Model):
    user = models.ForeignKey(User)
    team = models.ForeignKey(Team)
    extra_data = models.CharField()
    class Meta:
        unique_together = (('user','team'),)

Second question(s):

You could add a ranking field to the membership model and make 'user' and 'rank' unique_together, positive integer field. And user with rank == 1 is the admin. Or similar. It will be enforcing your scheme, but can be cumbersome to code the maintenance of it. You may well be better off with what you've got.

这篇关于Django中的许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆