模型保存方法数据检查未按预期工作

时间:2019-01-30 08:55:26

标签: django django-models django-forms django-rest-framework

我正在一片空白,也许您可​​以帮助我朝正确的方向前进。

模型方法:

def save(self, *args, **kwargs):
        query = Contract.objects.filter(rented = self.rented).exclude(id=self.id)
        check_data = gatekeeper_check(self, query)
        if check_data:
            super(Contract, self).save(*args, **kwargs)

功能:

def gatekeeper_check(self, query):
    current_contracts = {}
    for item in query:
        print(item)
        if (item.agreement_ends >= self.agreement_starts):
            current_contracts[item.id] = f'{item.agreement_starts} - {item.agreement_ends}'
            print(item.agreement_ends, item.actual_agreement_end, self.agreement_starts)
            raise ValidationError(mark_safe(f'Rentable already booked for the date range: <br>' + '<br>'.join({f'Contract ID: {k} | Start-End Date: {v}' for k, v in current_contracts.items()})))
        else:
            return True

结果不是我期望的:

Rentable already booked for the date range: Contract ID: 6UOj2RnNDX6jiGlC | Start-End Date: 2019-09-30 - 2019-10-29

在当前对象上:

开始日期:2019-01-29

结束日期:2019-04-29

理想的做法是节省这笔费用,因为这些天没有租房。但是它是从2019-09-30预订的

我在做什么错了?

0 个答案:

没有答案