Django 左连接或模拟
我不知道如何使用django orm进行左连接,请参阅下面的案例。
我的模型是(请不要介意母语):
class Production(models.Model):
'''Выработка.'''
project = models.ForeignKey(
Project, on_delete=models.PROTECT, verbose_name='проект')
timeperiod = models.ForeignKey(
Timeperiod, on_delete=models.PROTECT, verbose_name='период')
time = models.DurationField('трудочасы')
amount = models.DecimalField('выработка', max_digits=10, decimal_places=2)
note = models.TextField('заметка', blank=True, null=True)
is_auto = models.BooleanField(
'признак автоматического расчета', default=False)
class Timeperiod(models.Model):
'''Периоды.'''
start_date = models.DateField('дата начала')
end_date = models.DateField('дата окончания')
target_duration = models.DurationField('длительность')
is_open = models.BooleanField('открыт', default=True)
我想进行以下选择:
- 在start_date上使用过滤器和End_date在项目上获得相关生产的所有时间
- (如果有此特定时期的存在)
将其传递给 相关生产。对于template_processor并将其渲染为表(在循环中),
我知道如何过滤QuerySet,并且知道如何获得相关的QuerySet,但是我不明白如何过滤我的相关QuerySet
更新
我想要QUERYSET应该像此SQL查询一样:
SELECT *
FROM collector_timeperiod tp
LEFT JOIN collector_production AS p ON p.timeperiod_id = tp.id
AND p.project_id = 3
I can't figure out how to use Django ORM for left joins, please see case below.
My models are (please, don't mind native language):
class Production(models.Model):
'''Выработка.'''
project = models.ForeignKey(
Project, on_delete=models.PROTECT, verbose_name='проект')
timeperiod = models.ForeignKey(
Timeperiod, on_delete=models.PROTECT, verbose_name='период')
time = models.DurationField('трудочасы')
amount = models.DecimalField('выработка', max_digits=10, decimal_places=2)
note = models.TextField('заметка', blank=True, null=True)
is_auto = models.BooleanField(
'признак автоматического расчета', default=False)
class Timeperiod(models.Model):
'''Периоды.'''
start_date = models.DateField('дата начала')
end_date = models.DateField('дата окончания')
target_duration = models.DurationField('длительность')
is_open = models.BooleanField('открыт', default=True)
I would like to make following selection:
- Get all the Timeperiod with filters on start_date and end_date
- Get related Production with filter on project (if any exists for this particular period)
Pass it to template_processor and render it as a table (in for loop)
I know how to filter queryset, and know how to get related queryset, but I don’t understand how to filter my RELATED queryset
UPDATE
I want queryset that should be just like this SQL query:
SELECT *
FROM collector_timeperiod tp
LEFT JOIN collector_production AS p ON p.timeperiod_id = tp.id
AND p.project_id = 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
timePeriod__…
在相关模型上过滤:或者如果要检索
project
s:.distinct() doc]
将避免检索相同的
项目
,因为匹配timeperiod
s。You can filter on the related model with
timeperiod__…
, for example:or if you want to retrieve the
Project
s:The
.distinct()
call [Django-doc] will avoid retrieving the sameProject
that many times as there are matchingTimeperiod
s.