- Define the model and import the necessary modules:
from django.db import models
class Profile(models.Model):
# fields
class User(models.Model):
profile = models.OneToOneField(Profile, on_delete=models.CASCADE, null=True)
- Use Django's
exists
method to filter for users with an associated profile:
from myapp.models import User
users_with_profile = User.objects.filter(profile__isnull=False)
- Use Django's
empty
method to filter for users without an associated profile:
users_without_profile = User.objects.filter(profile__isnull=True)