Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#7488 closed (fixed)

Inline forms break when the foreign model does not inherit directly from models.Model

Reported by: mkibbel@… Owned by: brosner
Component: Core (Other) Version: master
Severity: Keywords: model inheritance
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:


This sample code example demonstrates the problem:

from django.db import models
from django.contrib import admin

class Base(models.Model):
   base_name = models.CharField(
      "Base Name",
      max_length = 40,

class Child(Base):
   child_name = models.CharField(
      "Child Name",
      max_length = 40,

class Inline(models.Model):
   child = models.ForeignKey(
   inline_name = models.CharField(
      "Inline Name",
      max_length = 40,

class InlineAdmin(admin.StackedInline):
   model = Inline

class ChildAdmin(admin.ModelAdmin):
   inlines = (InlineAdmin,)

try:, ChildAdmin)
except admin.sites.AlreadyRegistered:

When the admin's add_view for the "Child" class runs, a DoesNotExist exception is raised. If any of the following changes is made, the problem does not present itself:

  • removing the "edit_inline" functionality by removing "inlines" from ChildAdmin (not an acceptable workaround)
  • making the Child model inherit from models.Model (better, but still very inconvenient)

I traced the problem back to the get_query_set function in django.newforms.models.BaseInlineFormset, which reads as such:

    def get_queryset(self):
        Returns this FormSet's queryset, but restricted to children of 
        kwargs = { self.instance}
        return self.model._default_manager.filter(**kwargs)

Here, self.instance == Child(), an unsaved Child model instance (with no primary key), so the resulting call to filter is looking up an Inline object by an undefined primary key on the Child object. I have no idea why this problem does not present itself when Child inherits from models.Model, but I do have a simple patch to fix this bug.

Attachments (2)

baseinlineformset.patch (748 bytes) - added by mkibbel@… 7 years ago.
Patch to BaseInlineFormset.get_query_set
7899_tests.1.diff (724 bytes) - added by brosner 7 years ago.
failing tests

Download all attachments as: .zip

Change History (9)

Changed 7 years ago by mkibbel@…

Patch to BaseInlineFormset.get_query_set

comment:1 Changed 7 years ago by brosner

  • milestone set to 1.0 alpha
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to brosner
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 7 years ago by brosner

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 7 years ago by brosner

Let me do a quick brain dump here. Don't want to forget what I have tracked down thus far.

  • The DoesNotExist is coming from source:/branches/newforms-admin/django/db/models/fields/
  • The above is being called due to source:/branches/newforms-admin/django/db/models/fields/
  • Ultimately, Choice().poll does the right thing and raises the DoesNotExist exception. However when translating this into a queryset Choice.objects.filter(poll=Poll()) when Poll does not subclass something will evaluate to an empty list. But when the parent class (multi-table inheritance ) is introduced it will throw this DoesNotExist exception.
  • The patch here does indeed fix the problem, but I begin to wonder if this should be fixed upstream?

comment:4 Changed 7 years ago by telenieko

  • Component changed from Admin interface to Core framework
  • Keywords newforms-admin BaseInlineFormset removed
  • Patch needs improvement set
  • Version changed from newforms-admin to SVN

As per IRC conversation it seems this should be fixed up in the stack, so changing to trunk and querysets component.

comment:5 Changed 7 years ago by garcia_marc

  • milestone changed from 1.0 alpha to 1.0

So, if it's not a newforms-admin merge blocker bug, milestone should be 1.0 instead of 1.0 alpha.

Changed 7 years ago by brosner

failing tests

comment:6 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [8061]) Allow for matches against unsaved objects in querysets (which will therefore
match nothing). This allows for some more straightforward code in the admin

Fixed #7488 (all the debugging there was done by Brian Rosner, who narrowed it
down to the item in this patch).

comment:7 Changed 4 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top