[Answered ]-Compare values in two tables and create a list in Django

1👍

What I can Understand from you Data models, is that You want to Get those Employee Objects which are not present as Foreign key in RemEmployee Table.

So, you can find this by following query

Employee.objects.filter(rememployee__isnull=True).values_list('employeeno', flat=True)

Note: Although you have made Foreign key as unique in RemEmployee which makes it a OneToOne key(Not related to question but just telling)

1👍

Employee.objects.exclude(employeeno__in=RemEmployee.objects.values_list('employeeno', flat=True))

This will return all Employee objects whose employeeno isn’t shared by any RemEmployee objects.

Leave a comment