#7488 closed (fixed)
Inline forms break when the foreign model does not inherit directly from models.Model
Reported by: | 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)
Change History (10)
by , 17 years ago
Attachment: | baseinlineformset.patch added |
---|
comment:1 by , 17 years ago
milestone: | → 1.0 alpha |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 17 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 , 17 years ago
Component: | Admin interface → Core framework |
---|---|
Keywords: | newforms-admin BaseInlineFormset removed |
Patch needs improvement: | set |
Version: | newforms-admin → SVN |
As per IRC conversation it seems this should be fixed up in the stack, so changing to trunk and querysets component.
comment:5 by , 16 years ago
milestone: | 1.0 alpha → 1.0 |
---|
So, if it's not a newforms-admin merge blocker bug, milestone should be 1.0 instead of 1.0 alpha.
comment:6 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Patch to BaseInlineFormset.get_query_set