Opened 14 years ago

Closed 14 years ago

Last modified 4 months ago

#7488 closed (fixed)

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

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


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@… 14 years ago.
Patch to BaseInlineFormset.get_query_set
7899_tests.1.diff (724 bytes) - added by Brian Rosner 14 years ago.
failing tests

Download all attachments as: .zip

Change History (10)

Changed 14 years ago by mkibbel@…

Attachment: baseinlineformset.patch added

Patch to BaseInlineFormset.get_query_set

comment:1 Changed 14 years ago by Brian Rosner

milestone: 1.0 alpha
Owner: changed from nobody to Brian Rosner
Status: newassigned

comment:2 Changed 14 years ago by Brian Rosner

Triage Stage: UnreviewedAccepted

comment:3 Changed 14 years ago by Brian Rosner

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 14 years ago by Marc Fargas

Component: Admin interfaceCore framework
Keywords: newforms-admin BaseInlineFormset removed
Patch needs improvement: set
Version: newforms-adminSVN

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

comment:5 Changed 14 years ago by Marc Garcia

milestone: 1.0 alpha1.0

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

Changed 14 years ago by Brian Rosner

Attachment: 7899_tests.1.diff added

failing tests

comment:6 Changed 14 years ago by Malcolm Tredinnick

Resolution: fixed
Status: assignedclosed

(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 11 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

comment:8 Changed 4 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Easy pickings: unset
UI/UX: unset

In 18245b94:

Refs #7488, Refs #19524 -- Removed obsolete ModelInheritanceTest.test_issue_7488() test.

Obsolete since e9c24bef74e55729b190cf07e0ac452aa4c86fcd.

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