# Smart Field Filtering on Related Data

**URL:** <https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230>\
**Category:** Help me!\
**Created:** [April 3, 2021, 4:08pm UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230 "2021-04-03T16:08:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [April 3, 2021, 4:08pm UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/1 "2021-04-03T16:08:30Z")

</div>

Hi All -

The documentation shows how to implement filter logic on a smart field:

[https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#filtering](https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#filtering)

Is it possible to return filter conditions in the Smart Field definition that act on a related model’s data? The example only shows how to filter on the model’s own fields. (this is re: MySQL / Sequelize)

Thanks!

---

<div class="post-metadata">

**Author:** ![anon16419211](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anon16419211](https://community.forestadmin.com/u/anon16419211)\
**Post date:** [April 6, 2021, 8:20am UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/2 "2021-04-06T08:20:22Z")

</div>

Hello @yeti182 🙂

Basically I would say yes. Since you’re doing the code you want, you should be able to get any model and put your own logic.

What are you trying to achieve, can you give me more insights?

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [April 6, 2021, 12:33pm UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/3 "2021-04-06T12:33:27Z")

</div>

Great!

Sure thing. Lets say we have a model **Books** that is related to **Checkout Records**. There’s a Forest Admin Smart Field (enum type) on **Books** called _status_ that is one of “In-Hand,” “Checked Out,” or “Lost.”

If I wanted to write the filter logic for showing only “Lost” books, for example, I would need to write the filter logic on the _status_ Smart Field that looks for **Books** with **Checkout Records** that have a _return\_date_ of greater than a year ago.

Does that make sense? Thanks for your help!

---

<div class="post-metadata">

**Author:** ![anon16419211](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anon16419211](https://community.forestadmin.com/u/anon16419211)\
**Post date:** [April 6, 2021, 1:05pm UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/4 "2021-04-06T13:05:52Z")

</div>

To sum up a little bit you want to be able to filter on a **`book`**'s smart field called _status_ which will look at book’s related data called **`checkoutRecords`** and depends on the _return\_date_ data show the book’s records filling these conditions? 🙂

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [April 6, 2021, 1:18pm UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/5 "2021-04-06T13:18:14Z")

</div>

A perfect summary - that’s correct!

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [April 8, 2021, 10:37am UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/6 "2021-04-08T10:37:31Z")

</div>

Hello @yeti182,

Please find below an example of how you can interact with other collections in `filter` field

```javascript
    async filter({ condition, where }) {
      const genres = await models.genres.findAll({
        attributes: ['id'],
        where: {
          name: condition.value,
        },
        include: [{
          model: models.songs,
          attributes: ['id'],
        }],
      });

      let idsToReturn = genres.length ? genres[0].songs.map(song => song.id) : [];
      for (let index = 1; index < genres.length; index += 1) {
        const songIdsForCurrentGenre = genres[index].songs.map(song => song.id);
        idsToReturn = idsToReturn.filter(songId => songIdsForCurrentGenre.includes(songId));
      }

      return {
        id: idsToReturn,
      };
    },

```

You will need to add some logic to handle different operators you want to supports as described in the documentation

Hope it helps

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [April 8, 2021, 3:14pm UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/7 "2021-04-08T15:14:10Z")

</div>

Thanks, @anon90145840 - this is very helpful. I think it would be great to show [in the documentation](https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#filtering) that it’s possible to return an array of IDs in a filter function for a SmartField!

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [April 8, 2021, 3:15pm UTC](https://community.forestadmin.com/t/smart-field-filtering-on-related-data/2230/8 "2021-04-08T15:15:27Z")

</div>

I’ll let the team know about your suggestion, thank you!
