'Django ORM equivalent to left exclusive JOIN

Relatively new to Django and I have the following models in my django (3.2) project:

class projectsA(models.Model):
    projectID_A = models.BigIntegerField(primary_key=True)
    project_name_A = models.TextField(blank=True, null=True)
    project_desc_A = models.TextField(blank=True, null=True)
    projectID_B = models.ForeignKey('projectsB', models.DO_NOTHING, db_column='projectID_B', blank=True, null=True, db_constraint=False)

    class Meta:
       managed = False
       db_table = 'projectsA'

class projectsB(models.Model):
    projectID_B = models.BigIntegerField(primary_key=True)
    project_name_B = models.TextField(blank=True, null=True)
    project_desc_A = models.TextField(blank=True, null=True)
    
    class Meta:
      managed = False
      db_table = 'projectsB'

I want to get all projects from projectsA which are not in projectsB. The SQL equivalent would be

SELECT * FROM projectsA a LEFT JOIN projectsB b on a.projektID_B = b.projectID_B WHERE b.projectID_B is null

I have tried with .exclude or .select_related() and several other approaches, but cannot find a solution for this scenario. Thanks in advance.



Solution 1:[1]

It should be something like:

queryset = projectsA.objects.filter(projektID_B__isnull=True)

You can read about isnull here https://docs.djangoproject.com/en/4.0/ref/models/querysets/#isnull

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Bartosz Stasiak