Django 通过集合查询

发布于 2024-10-31 07:03:09 字数 681 浏览 0 评论 0原文

我是 Django 新手,我一直在通过多个集合进行查询。

我有三个模型;

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

class AppointmentGroup(models.Model):
    name = models.CharField(max_length = 100) # not used in design.. delete when not used at the end of the project
    project = models.ForeignKey(Project)
    location = models.ForeignKey(Location)

class Appointment(models.Model):
    appointment_group = models.ForeignKey(AppointmentGroup)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

现在我想要一个返回的对象集,其中仅包含在特定年份内有约会的项目。并且项目对象中的约会集对象仅包含当年的约会集对象!

使用 django 查询很容易做到这一点,还是我必须逐个循环项目并检查日期的所有约会?

I'm new to Django and I'm stuck at querying through multiple sets.

I have three models;

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

class AppointmentGroup(models.Model):
    name = models.CharField(max_length = 100) # not used in design.. delete when not used at the end of the project
    project = models.ForeignKey(Project)
    location = models.ForeignKey(Location)

class Appointment(models.Model):
    appointment_group = models.ForeignKey(AppointmentGroup)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

Now I want a returned object set with only the projects that have appointments within a particular year. And that the appointment set objects in the project object contains only the ones in that year!

Is this easy to do with a django query or must i loop through the projects one by one and check all the appointments on the date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2024-11-07 07:03:09

我猜预约模式与您的项目有一定的相关性,而您只是忽略了这一点。

您可能想使用 range 跨越关系的查找

import datetime
start = datetime.date(2010, 1, 1)
end = datetime.date(2010, 12, 31)
projects_in_2010 = Projects.objects.filter(appointmentgroup__appointment__start_date__range(start, end))

I'm guessing that the appointment model is some how related to your projects and you just left that off.

You probably want to use range and lookups that span relationships:

import datetime
start = datetime.date(2010, 1, 1)
end = datetime.date(2010, 12, 31)
projects_in_2010 = Projects.objects.filter(appointmentgroup__appointment__start_date__range(start, end))
美人骨 2024-11-07 07:03:09

试试这个

AppointmentGroup.objects.filter(appoinment_set__start_date__year=2011, appoinment_set__end_date__year=2011)

Try this

AppointmentGroup.objects.filter(appoinment_set__start_date__year=2011, appoinment_set__end_date__year=2011)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文