如何修复Django查询中两个日期范围是否重叠

2024-09-28 16:57:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个三个模型,看起来像这样:


class BuildingCell(models.Model):
    building_name = models.CharField(max_length=100, blank=True, null=True)
    publish = models.DateField(auto_now=False)
    cell_name = models.CharField(max_length=100, blank=True, null=True)
    ...

class InputCutSew(models.Model):
    user = models.ForeignKey(Profile, blank=True, null=True, on_delete=models.SET_NULL)
    cell = models.ForeignKey(BuildingCell, blank=True, null=True, on_delete=models.SET_NULL)
    publish = models.DateField(auto_now=False)
    ...

class Absent(models.Model):
    cell = models.ForeignKey(BuildingCell, blank=True, null=True, on_delete=models.SET_NULL)
    building_name = models.CharField(max_length=12, blank=True, null=True)
    publish = models.DateField(auto_now=False)
    ...

#my queryset
q1 = BuildingCell.objects.filter(inputcutsew__publish__range=['2019-08-01', '2019-01-03'], 
absent__publish__range=['2019-08-01', '2019-01-03'], absent__building_name='f1', 
absent__bagian='CUT', inputtcutsew__user='1').exclude(inputcutsew__cell_name__isnull=True)/
.exclude(inputcutsew__cell_name__exact='')/ 
.order_by('inputcutsew__cell')/
.values('inputcutsew__cell', 'inputcutsew__model', 'absent__cell', 'inputcutsew__output')
#my error result:
'inputcutsew__cell', 'inputcutsew__model', 'absent__cell', 'inputcutsew__output
    1a                   aa                     1a             122
    1a                   zs                     1a             129 
    1b                   as                     1b             100
    1a                   aa                     1a             122
    1b                   as                     1b             100
    1a                   zs                     1a             129 

我想通过查询从三个模型中得到一些字段,但结果仍然是两倍。你能给我一些建议吗??,任何帮助都将不胜感激。你知道吗


Tags: nametruemodelmodelscellpublishnullmax