检查重叠间隔整数字段验证 django python

Check Overlapping intervals integerfield validation django python

谁能帮我解决 django 中的重叠验证问题

moldel.py

 start = IntegerRangeField()
 end = IntegerRangeField()

form.py

class CheckForm(forms.ModelForm): def clean(self):

start=cleaned_data.get("start")
    end = cleaned_data.get("end")
    conflicts = Check.objects.filter(
        start_bbch__lte=end,
        end_end__gte=start,
    )
    if any(conflicts):
        raise forms.ValidationError(_("overlapping not allowed" % conflicts.count()))
    return cleaned_data

我认为要检查重叠,您需要确保新对象的开始和结束都不在现有间隔内。

所以我建议这样:

conflicts = Check.objects.filter(
    start_bbch__gte=start, end_end__lte=start
) | Check.objects.filter(
    start_bbch__gte=end, end_end__lte=end
)