如何解决Django Queryset中两个日期范围重叠的问题

时间:2019-08-29 00:37:21

标签: python sql django postgresql

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


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 

我想查询从三个模型中获取一些字段,但结果仍然是两倍。您能对此提出建议吗?,将不胜感激。

0 个答案:

没有答案