在检查多对多关系的同时检查另一个字段
我的一个模型有一个简单的所有权设计。它可以由多人拥有,当前所有者可以添加其他人,但他们必须确认邀请才能被视为真正的所有者。
class MyOwnedThing(models.Model):
owners = models.ManyToManyField(User, through='Ownership', related_name='othings')
def is_owner(self, user):
return user in self.owners
class Ownership(models.Model):
user = models.ForeignKey(User)
myownedthing = models.ForeignKey(MyOwnedThing)
confirmed = models.BooleanField(default=False)
问题是 MyOwnedThing.is_owner
需要检查所有者是否已确认他们的邀请。有没有一种简单的方法可以做到这一点,或者我是否在 Ownership.objects.filter(user=u, myownedthing=mot,确认=True)
周围进行单独的尝试/例外?
I have a simple-ish ownership design on one of my models. It can be owned by multiple people and current owners can add other people but they have to confirm the invite before they are treated as a real owner.
class MyOwnedThing(models.Model):
owners = models.ManyToManyField(User, through='Ownership', related_name='othings')
def is_owner(self, user):
return user in self.owners
class Ownership(models.Model):
user = models.ForeignKey(User)
myownedthing = models.ForeignKey(MyOwnedThing)
confirmed = models.BooleanField(default=False)
The problem is MyOwnedThing.is_owner
needs to check that the owner had confirmed their invite. Is there a simple way of doing that or am I left doing a separate try/except around Ownership.objects.filter(user=u, myownedthing=mot, confirmed=True)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常使用关联表来实现此类功能。以下内容尚未经过测试,但应该可以让您大致了解我的意思:
I typically use association tables for this type of functionality. The following hasn't been tested but should give you a general idea of what I mean: