【发布时间】:2014-06-03 21:10:46
【问题描述】:
我有一个User 模型和一个Event 模型。我对两者都有以下内容:
class Event(models.Model):
...
timestamp = models.DateTimeField()
organization_map = models.ManyToManyField(Organization)
class User(AuthUser):
...
subscribed_orgs = models.ManyToManyField('Organization')
我想查找在特定时间范围内创建的所有事件并查找订阅这些组织的用户。我知道如何为此编写 SQL(这很容易),但是使用 Django ORM 执行此操作的 pythonic 方式是什么?
我正在尝试如下:
orgs = Organization.objects.all()
events = Event.objects.filter(timestamp__gt=min_time) # Min time is the time I want to start from
events = events.filter(organization_map__in=orgs)
但是从那里,我如何映射到订阅了 organization 的用户?
我正在尝试像这样映射它:
users = User.objects.filter(subscribed_orgs__in=...
【问题讨论】: