# n+1 on Admin \> Inline. prefetch\_related no working?

**URL:** https://forum.djangoproject.com/t/n-1-on-admin-inline-prefetch-related-no-working/40803
**Category:** Using the ORM
**Created:** [May 6, 2025, 5:54pm UTC](https://forum.djangoproject.com/t/n-1-on-admin-inline-prefetch-related-no-working/40803 "2025-05-06T17:54:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![FloppyDisco](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/floppydisco/32/28414_2.png) [@FloppyDisco](https://forum.djangoproject.com/u/FloppyDisco)
#### Post date: [May 6, 2025, 5:54pm UTC](https://forum.djangoproject.com/t/n-1-on-admin-inline-prefetch-related-no-working/40803/1 "2025-05-06T17:54:38Z")

</div>

Forgive my ignorance here but i have been scratching my head for several hours at this point.

i have some related models.

```python
class Course(models.Model):
    name = models.CharField(max_length=100)

class ClassMeeting(models.Model):
    course = models.ForeignKey(Course, related_name='meetings', on_delete=models.CASCADE)
    # day of week and time i.e "wednesdays at 4pm". 

class Class(models.Model):
    course = models.ForeignKey(Course, related_name='classes', on_delete=models.CASCADE)
    class_meeting = models.ForeignKey(ClassMeeting, on_delete=models.CASCADE)
    class_date = models.DateField()
    # this is the actual day a class occurs "jan 8th"

```

someone signs up for `Physics Wed @ 6pm` but the actual classes become `'jan 1st' , 'jan 8th', etc...`

to create the courses and their related class schedules for each semester i have a CourseAdmin with two inlines one for ClassMeetings and one for Classes

the ClassMeetingInline does not cause any issues, only like 1 or 2 extra queries.  
however the ClassInline is causing an N+1 and i don’t know how to stop it.

the N+1 occurs when i add the ‘class\_meeting’ field to the inline form. without this field it does not happen, and the queries are all for fetching ClassMeeting data.

I am not sure where to put the prefetch\_related so django can find the info it needs in the queryset.

i believe there is a `get_queryset` on both the `CourseAdmin` and the `ClassInline` i have tried adding `prefetch_related("meetings")` to the CourseAdmin and `prefetch_related("class_meeting")` to the `ClassInline`.  
i have also tried adding `prefetch_related("classes__class_meeting")` to the `CourseAdmin`

i’ve tried a few GPT solutions, that obviously didn’t work otherwise i wouldn’t be here, which had to do with overriding `get_formset` and others.

i am using debug toolbar, and i have commented out all my ` __str__ ()` methods to make sure it is not coming from those.

it’s a bit of a weird one because the relationship is a bit circular.  
any advice would be appreciated.

thanks.

---

<div class="post-metadata">

### Author: ![TomHall2020](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tomhall2020/32/26707_2.png) [@TomHall2020](https://forum.djangoproject.com/u/TomHall2020)
#### Post date: [May 8, 2025, 12:02pm UTC](https://forum.djangoproject.com/t/n-1-on-admin-inline-prefetch-related-no-working/40803/2 "2025-05-08T12:02:37Z")

</div>

It would make it easier for others to help if you included the code for your admin classes as well as the model definitions. Both the original definitions where you are seeing the N+1 problem, and some of the solutions you tried with adding `prefetch_related` or overriding `get_formset`.

As a starter I have been able to solve some N+1 queries in the admin by overriding `get_queryset` on the parent ModelAdmin.
