[Django]-Model integrity check in django

0👍

You need to use Q for complex queries.

from django.db.models import Q
_exists =   Entry.objects.filter(Q(
                     Q(Q(start__gte=entry.start) & Q(start__lte=entry.end)) | 
                     Q(Q(end__gte=entry.start) & Q(end__lte=entry.end)) | 
                     Q(Q(start__lte=enrty.start) & Q(end__gte=entry.end))
))
if _exists:
    "There is existing event"
else:
    "You can create the event"

Since I do not test this, I use Q objects whereever I thought would be necessary.

Using this query, you will not need any unique check.

👤Mp0int

Leave a comment