from django.db import models
from django.db.models.functions import Now, TruncDay
class Foo(models.Model):
start_date = models.DateTimeField()
end_date = models.DateTimeField()
class Meta:
constraints = [
name="start_date must be greater than or equal today",
check=CheckConstraints(start_date__gte=TruncDay(Now()))
]
like above code, I want to add CheckConstraint to check whether start_date is greater than or equal with today.
but, after makemigration and migrate, error happened.
functions or expression 'CURRENT_TIME' cannot be userd in check clause
I tried two ways.
first.
check = CheckConstraints(start_date__gte=timezone.now())
but, Migration File has result of timezone.now() method, like datetime(2022, 01, 06, tzinfo=<UTC>)
second.
check = CheckConstraints(start_date__gte=timezone.now)
and
check = CheckConstraints(start_date__gte=TruncDay(Now))
this trial make error function cannot be resolved
when I tried to migrate.
How can I check start_date and today?
thanks and apologize for my English skills in advance.