How to exclude objects connected via ForeignKey Django?
class AnimalX(models.Model):
my_animal = models.ForeignKey('animals.MyAnimal', on_delete=models.CASCADE, null=False, blank=False, related_name='animals')
class MyAnimal(models.Model):
name = models.CharField(max_length=256)
I'd like to get all the MyAnimal instances that do not have AnimalX instance.
Do you have any idea how can i achieve this? I thought about doing MyAnimal.objects.all().exclude(AnimalX.objects.all()) but it doesnt work.
You can try this:
MyAnimal.objects.filter(animals=None)