Opened 16 years ago

Closed 16 years ago

Last modified 3 years 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

Description

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(
      Child,
   )
   inline_name = models.CharField(
      "Inline Name",
      max_length = 40,
   )

class InlineAdmin(admin.StackedInline):
   model = Inline

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

try:
   admin.site.register(Child, ChildAdmin)
except admin.sites.AlreadyRegistered:
   pass

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 
        self.instance
        """
        kwargs = {self.fk.name: 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@… 16 years ago.
Patch to BaseInlineFormset.get_query_set
7899_tests.1.diff (724 bytes ) - added by Brian Rosner 16 years ago.
failing tests

Download all attachments as: .zip

Change History (10)

by mkibbel@…, 16 years ago

Attachment: baseinlineformset.patch added

Patch to BaseInlineFormset.get_query_set

comment:1 by Brian Rosner, 16 years ago

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

comment:2 by Brian Rosner, 16 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Brian Rosner, 16 years ago

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/related.py#L231
  • The above is being called due to source:/branches/newforms-admin/django/db/models/fields/related.py#L137
  • 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 by Marc Fargas, 16 years ago

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 by Marc Garcia, 16 years ago

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.

by Brian Rosner, 16 years ago

Attachment: 7899_tests.1.diff added

failing tests

comment:6 by Malcolm Tredinnick, 16 years ago

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
interface.

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

comment:7 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

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