Q: Django : Find 'difference' between two models Say I have two models as follows -
class DID_Definition_Model(models.Model):   # DID to Region-Carrier Mapping
    did_number= models.CharField(max_length=32, validators=[alphanumeric], primary_key=True)




class DID_Number_Assignment_Model(models.Model):       #DID Number Assignment
    did_selector = models.ForeignKey(DID_Definition_Model, on_delete=models.CASCADE, primary_key=True, unique=True)


I want to report on the 'difference' in entries, (possibly via difference method)  which may be available in DID_Definition_Model did_number field but not in DID_Number_Assignment_Model did_selector field.
In my failing attempt my thought process was to create a query which will contain all entries of did_number. Then repeat the same procedure to get all entries of did_selector. Finally compare the two queries using difference method.
But I kind of got stuck at the first stage -


*

*


getattr(DID_Definition_Model, did_number)
      Traceback (most recent call last):
      File "", line 1, in 
      NameError: name 'did_number' is not defined *
      But the above is simply not possible as did_number is a valid field of DID_Definition_Model



Any guidance will be helpful as I have been through multiple message boards and could not seem to find any way out.

A: To find all objects where there are no related objects, you can use relationship__isnull=True.
defs_with_no_assignments = DID_Definition_Model.objects.filter(
    did_number_assignment_model__isnull=True
)

To get just the values, you can use values_list
did_numbers_with_no_assignments.values_list('did_number', flat=True)

