Django queryset filtering, comparing fields with fields

I would like to filter a queryset where the homecountry is the same as the residentcountry. I try to do something like this:

users = User.objects.filter(homecountry=residentcountry)

How can I make this work?

Considering homecountry and residentcountry are two fields in your table. F expression can be used for this. Please check the documentation...

from django.db.models import F

users = User.objects.filter(homecountry=F('residentcountry'))
Back to Top