filter rows from table where field from foreign table is of a value

I have a Job model where I have an active field where I can set if its active or not and show only jobs where active is True.

class Job(models.Model):
    title = models.CharField(max_length=250)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

But I want to filter jobs for all users that are not active.

# query = Q(user__in=####)
jobs = Job.objects.all().filter(query)

For clarification here - are you saying you want all Jobs related to an instance of User where the is_active attribute is True?

Yes, I got it solved using :
query = Q(user__is_active=True)
I guess this is the accepted approach ?

What makes you think that it might not be? (What makes you concerned about this?)

1 Like