# Search for and add reverse foreign-keys

**URL:** <https://forum.djangoproject.com/t/search-for-and-add-reverse-foreign-keys/30400>\
**Category:** The Admin\
**Created:** [April 22, 2024, 12:13am UTC](https://forum.djangoproject.com/t/search-for-and-add-reverse-foreign-keys/30400 "2024-04-22T00:13:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![willard](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/willard/32/20780_2.png) [@willard](https://forum.djangoproject.com/u/willard)\
**Post date:** [April 22, 2024, 12:13am UTC](https://forum.djangoproject.com/t/search-for-and-add-reverse-foreign-keys/30400/1 "2024-04-22T00:13:30Z")

</div>

Hello, I’ve got two models in a many-to-many relationship. One is a Node, which can have many different Buildings, and one is a Building, which can have many different nodes.

[Buildings](https://github.com/nycmeshnet/meshdb/blob/main/src/meshapi/models/building.py#L79-L97) hold the Field for Nodes. On the `Add` page and `Change` page, I can use `filter_horizontal` to search for and add nodes to a Building:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/9/4/946c346759bf6c0135cc6a35b1f3f424dd2f706c.png)

However, I am unable to do this in reverse, from the Node. I have a ([somewhat customized](https://github.com/nycmeshnet/meshdb/blob/main/src/meshapi/admin.py#L44-L53)) Inline which lets me view building details, but I’d like to achieve the above on the Node object so that I can go the other way.

Anyone know how to do this? Thanks in advance! 😃

---

<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:** [April 22, 2024, 2:14am UTC](https://forum.djangoproject.com/t/search-for-and-add-reverse-foreign-keys/30400/2 "2024-04-22T02:14:30Z")

</div>

The Django admin docs for [working with many-to-many models](https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#working-with-many-to-many-models) covers this case of using an inline admin on both sides of a many-to-many relationship.

Side note: In the future, please post the actual code being discussed here instead of links to external repositories. When you do so, copy/paste the code into the body of your post, between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. (This forces the forum software to keep your code properly formatted.)

---

<div class="post-metadata">

**Author:** ![willard](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/willard/32/20780_2.png) [@willard](https://forum.djangoproject.com/u/willard)\
**Post date:** [April 22, 2024, 10:19pm UTC](https://forum.djangoproject.com/t/search-for-and-add-reverse-foreign-keys/30400/3 "2024-04-22T22:19:25Z")

</div>

Ah sorry about that, will do better in the future.

That’s just about what I want, thanks! Is there a way to use a `filter_horizontal` to select a building? The dropdown has \>10k entries 😅

Here’s what I’ve got right now:

```auto
class MembershipInline(admin.TabularInline):
    model = Building.nodes.through
    extra = 0

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/7/7/777324fe98a999c9cd24bdd819a9d41006571f8e.png)

---

<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:** [April 25, 2024, 6:13am UTC](https://forum.djangoproject.com/t/search-for-and-add-reverse-foreign-keys/30400/4 "2024-04-25T06:13:22Z")

</div>

> [@willard](#):
>
> Is there a way to use a `filter_horizontal` to select a building?

I don’t know from direct experience, but the docs at [The Django admin site | Django documentation | Django](https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#inlinemodeladmin-options) certainly give me the impression you ought to be able to.

---

<div class="post-metadata">

**Author:** ![willard](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/willard/32/20780_2.png) [@willard](https://forum.djangoproject.com/u/willard)\
**Post date:** [April 26, 2024, 4:50am UTC](https://forum.djangoproject.com/t/search-for-and-add-reverse-foreign-keys/30400/5 "2024-04-26T04:50:46Z")

</div>

Haha! Success!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/e/2/e296e2282b60332adff831cbb5e1293cac378848.png)

I actually needed to add an `autocomplete_fields`! I’m sure I could configure this to use `filter_horizontal` as well, but now I might try to combine this widget with another one to save some space 😉

Here’s what worked.

```auto
class MembershipInline(admin.TabularInline):
    model = Building.nodes.through
    extra = 0
    autocomplete_fields = ["building_id"]

```
