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

**URL:** <https://forum.djangoproject.com/t/filter-rows-from-table-where-field-from-foreign-table-is-of-a-value/17910>\
**Category:** Using the ORM\
**Created:** [December 30, 2022, 2:51am UTC](https://forum.djangoproject.com/t/filter-rows-from-table-where-field-from-foreign-table-is-of-a-value/17910 "2022-12-30T02:51:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [December 30, 2022, 2:51am UTC](https://forum.djangoproject.com/t/filter-rows-from-table-where-field-from-foreign-table-is-of-a-value/17910/1 "2022-12-30T02:51:54Z")

</div>

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.

```auto
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.

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 30, 2022, 3:25am UTC](https://forum.djangoproject.com/t/filter-rows-from-table-where-field-from-foreign-table-is-of-a-value/17910/2 "2022-12-30T03:25:35Z")

</div>

For clarification here - are you saying you want all Jobs related to an instance of `User` where the [`is_active`](https://docs.djangoproject.com/en/4.1/ref/contrib/auth/#django.contrib.auth.models.User.is_active) attribute is True?

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [December 30, 2022, 3:27am UTC](https://forum.djangoproject.com/t/filter-rows-from-table-where-field-from-foreign-table-is-of-a-value/17910/3 "2022-12-30T03:27:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 30, 2022, 3:32am UTC](https://forum.djangoproject.com/t/filter-rows-from-table-where-field-from-foreign-table-is-of-a-value/17910/4 "2022-12-30T03:32:16Z")

</div>

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