'In django models, how to add a constraint that looks across multiple rows

In the model below, I define a user, a habit the user is trying out, the start date (required) and end date (if the habit has ended). I want to add a constraint that each user can only be actively in one habit at a time. There is conceptually a few ways to determine this: For a specific user, only one habit has a NULL end date or for a specific user, the start-end range does not overlap for any of their habits, etc. I'm not sure how to approach writing this constraint, since it is looking across multiple rows instead of checking validity within one row. Any help getting started would be appreciated.

from django.db import models
from django.contrib.auth.models import User
from django.db.models import F, Q

class HabitChoices(models.TextChoices):
    NUTRITION = 'nutrition', 'Nutrition'
    EXERCISE = 'exercise', 'Exercise'
    SLEEP = 'sleep', 'Sleep'
    STRESS = 'stress', 'Stress'
    ALCOHOL = 'alcohol', 'Alcohol'
    SMOKING = 'smoking', 'Smoking'

class HabitStatus(models.Model):
    """ This model keeps track, at a high level, of which habit the user is currently in.
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    habit_name = models.CharField(max_length=10, choices=HabitChoices.choices, default=HabitChoices.NUTRITION)
    start_date = models.DateField()
    end_date = models.DateField(null=True, blank=True)

    def is_current_habit(self):
        if self.end_date == None:
            return True
        else:
            return False
    
    class Meta:
        constraints = [
            models.CheckConstraint(
                check=Q(start_date_lte=F('end'), start_date_gte=Now),
                name='correct_datetime'
            )
        ]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source