# models.ForeignKey caused too many SQL queries

**URL:** <https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649>\
**Category:** The Admin\
**Created:** [March 24, 2020, 9:40pm UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649 "2020-03-24T21:40:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simpleopen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/simpleopen/32/2778_2.png) [@simpleopen](https://forum.djangoproject.com/u/simpleopen)\
**Post date:** [March 24, 2020, 9:40pm UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649/1 "2020-03-24T21:40:14Z")

</div>

A _admin.StackedInline_ class uses a _Model_ class, and the _Model_ class has a foreign key (_models.ForeignKey_). This foreign key is generating too many SQL queries, as it tries to make a select list containing all items in the referred-to table (ProductTemplate).

Is there a way to restrict the foreign key to only list one item, instead of all items in the referred-to table?

Any other suggestions will also be highly appreciated. Let me know if you need more details.

**Partial source code:**

```
In file admin.py:
...
class TransactionAttributeInlineAttribute(admin.StackedInline):
    model = models.TransactionAttribute
    verbose_name = 'Transaction Link'
    verbose_name_plural = 'Product Links'
class MyAttributeAdmin(admin.ModelAdmin):
    inlines = [TransactionAttributeInlineAttribute]
    list_display = ['name','datatype','required']
...

In file models.py:
...
class TransactionAttribute(models.Model):
    product_template = models.ForeignKey(ProductTemplate)
...

```

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 24, 2020, 10:27pm UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649/2 "2020-03-24T22:27:22Z")

</div>

Hi @simpleopen, welcome to the forum

Are you not looking for the `max_num` attribute on an inline? [https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.max\_num](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.max_num)

---

<div class="post-metadata">

**Author:** ![simpleopen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/simpleopen/32/2778_2.png) [@simpleopen](https://forum.djangoproject.com/u/simpleopen)\
**Post date:** [March 25, 2020, 12:23am UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649/3 "2020-03-25T00:23:38Z")

</div>

Hi @adamchainz , thank you for your reply.

I tested max\_num like below, see some change but not yet the one I want. It limits the number of items for “_TransactionAttribute_”, but still a big list generated by foreign key.

Or, should I try something like filtering the results returned by foreign key?

Thank you for your help.

**Source code change:**

```
class TransactionAttributeInlineAttribute(admin.StackedInline):
    model = models.TransactionAttribute
    max_num = 1
    verbose_name = 'Transaction Link'
    verbose_name_plural = 'Product Links'
```

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 25, 2020, 10:40am UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649/4 "2020-03-25T10:40:24Z")

</div>

Hi, I’m afraid I don’t follow the question at this point.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [March 25, 2020, 11:26am UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649/5 "2020-03-25T11:26:52Z")

</div>

> Is there a way to restrict the foreign key to only list one item, instead of all items in the referred-to table?

I’m guessing this is an issue with a `ModelChoiceField` generating the `choices` for the form widget.

If so, [`ModelAdmin.raw_id_fields`](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields) is a possibility.

HTH

---

<div class="post-metadata">

**Author:** ![timonweb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/timonweb/32/570_2.png) [@timonweb](https://forum.djangoproject.com/u/timonweb)\
**Post date:** [March 25, 2020, 2:47pm UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649/6 "2020-03-25T14:47:56Z")

</div>

Or [ModelAdmin.autocomplete\_fields](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields) would be even more user friendly. It gives you a nice `select2` based autocomplete widget.

---

<div class="post-metadata">

**Author:** ![simpleopen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/simpleopen/32/2778_2.png) [@simpleopen](https://forum.djangoproject.com/u/simpleopen)\
**Post date:** [March 25, 2020, 9:10pm UTC](https://forum.djangoproject.com/t/models-foreignkey-caused-too-many-sql-queries/1649/7 "2020-03-25T21:10:46Z")

</div>

Thank you, @carltongibson . Yes, your advice works.

It’s a system performance issue, and the culprit was Django’s default behavior in handling database foreign key.

In my case, table of _attribute_ (_TransactionAttribute_) has a foreign key linking to the one _product_ (_ProductTemplate_) using this attribute. By default Django makes a drop-down select enumerating all products, not only the linked one but also others unrelated.

We noticed the issue on attribute _First Name_ as it is used by roughly all 100 products, which means to list 100 product links and each link has a drop-down with 100 selections. The overall complexity is on the level of square (100 x 100), and too many SQL queries overwhelmed database.

Another attribute _Last Name_ was also broken for the same reason, while the less-used attributes are still OK.

I fix the issue with your suggestion: get rid of drop-down select, each product link shows only the one linked product, not the others.

Also many thanks to the other replies.
