2

I would like to make impossible to insert data to my database if that data conflicts with some another data that already exists in my base. What I mean:

Let's say that I would like to insert into database fields start_date and end_date with the following condition:

-period (start_date; end_date) cannot intersect with any other periods in my base

Is any nice and fast way to check it in django?

Possible I can iterate over whole database manually, but maybe is there any nice wrapper?

4

1 回答 1

1

我建议执行以下操作:

lower = Entry.objects.filter(start_date__lte=new_start)
lower = lower.filter(end_date_gte=new_start)

upper = Entry.objects.filter(start_date__lte=new_end)
upper = upper.filter(end_date_gte=new_end)

valid = len(lower) == 0 and len(upper) == 0

if valid:
    # Do the insert
    pass
于 2013-04-22T01:46:07.843 回答