Django 左连接或模拟

发布于 2025-01-17 18:58:45 字数 1357 浏览 1 评论 0原文

我不知道如何使用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)

我想进行以下选择:

  1. 在start_date上使用过滤器和End_date在项目上获得相关生产的所有时间
  2. (如果有此特定时期的存在)

将其传递给 相关生产。对于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:

  1. Get all the Timeperiod with filters on start_date and end_date
  2. 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 技术交流群。

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

发布评论

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

评论(1

呆° 2025-01-24 18:58:45

您可以使用timePeriod__…在相关模型上过滤:

Production.objects.filter(
    timeperiod__start_date__gte='2022-3-1',
    timeperiod__end_date__lte='2022-3-31'
)

或者如果要检索project s:

Project.objects.filter(
    production__timeperiod__start_date__gte='2022-3-1',
    production__timeperiod__end_date__lte='2022-3-31'
).distinct()

.distinct() doc]将避免检索相同的项目,因为匹配timeperiod s。

You can filter on the related model with timeperiod__…, for example:

Production.objects.filter(
    timeperiod__start_date__gte='2022-3-1',
    timeperiod__end_date__lte='2022-3-31'
)

or if you want to retrieve the Projects:

Project.objects.filter(
    production__timeperiod__start_date__gte='2022-3-1',
    production__timeperiod__end_date__lte='2022-3-31'
).distinct()

The .distinct() call [Django-doc] will avoid retrieving the same Project that many times as there are matching Timeperiods.

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