Django 通过集合查询
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜预约模式与您的项目有一定的相关性,而您只是忽略了这一点。
您可能想使用
range
和跨越关系的查找
: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
andlookups that span relationships
:试试这个
Try this