Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#11120 closed (fixed)

Changeset 10756 breaks foreign key inline for multi-table subclasses

Reported by: George Song Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: multi table inheritance foreignkey inline
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In [10756] BaseInlineFormSet.add_fields() added line 756:

to_field=self.fk.rel.field_name,

This causes ForeignKey inlines for multi-table subclasses to break in the Admin.

Test models.py:

from django.db import models

class Place(models.Model):
    name = models.CharField(max_length=50)

class Restaurant(Place):
    pass

class Manager(models.Model):
    retaurant = models.ForeignKey(Restaurant)
    name = models.CharField(max_length=50)

Test admin.py:

from django.contrib import admin

from models import *

class ManagerInline(admin.StackedInline):
    model = Manager

class RestaurantAdmin(admin.ModelAdmin):
    inlines = [ManagerInline]

admin.site.register(Restaurant, RestaurantAdmin)

Going to /admin/inline/restaurant/add/ causes the following stack trace:

Traceback:
File "E:\lehrhaus\django\django\core\handlers\base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "E:\lehrhaus\django\django\contrib\admin\options.py" in wrapper
  226.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "E:\lehrhaus\django\django\contrib\admin\sites.py" in inner
  184.             return view(request, *args, **kwargs)
File "E:\lehrhaus\django\django\db\transaction.py" in _commit_on_success
  240.                 res = func(*args, **kw)
File "E:\lehrhaus\django\django\contrib\admin\options.py" in add_view
  758.                 formset = FormSet(instance=self.model(), prefix=prefix)
File "E:\lehrhaus\django\django\forms\models.py" in __init__
  707.                                                 queryset=qs)
File "E:\lehrhaus\django\django\forms\models.py" in __init__
  459.         super(BaseModelFormSet, self).__init__(**defaults)
File "E:\lehrhaus\django\django\forms\formsets.py" in __init__
  44.         self._construct_forms()
File "E:\lehrhaus\django\django\forms\formsets.py" in _construct_forms
  88.             self.forms.append(self._construct_form(i))
File "E:\lehrhaus\django\django\forms\models.py" in _construct_form
  720.         form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File "E:\lehrhaus\django\django\forms\models.py" in _construct_form
  470.         return super(BaseModelFormSet, self)._construct_form(i, **kwargs)
File "E:\lehrhaus\django\django\forms\formsets.py" in _construct_form
  108.         self.add_fields(form, i)
File "E:\lehrhaus\django\django\forms\models.py" in add_fields
  757.                 label=getattr(form.fields.get(self.fk.name), 'label', capfirst(self.fk.verbose_name))
File "E:\lehrhaus\django\django\forms\models.py" in __init__
  856.                 kwargs["initial"] = getattr(self.parent_instance, self.to_field)
File "E:\lehrhaus\django\django\db\models\fields\related.py" in __get__
  244.                 raise self.field.rel.to.DoesNotExist

Exception Type: DoesNotExist at /admin/inline/restaurant/add/
Exception Value: 

Commenting out line 756 makes everything work again.

Change History (7)

comment:1 by Russell Keith-Magee, 15 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

It isn't quite as simple as the ticket describes - commenting out line 756 breaks the test case that the line was introduced to support; even if you do comment out the line, you can't save any new inline instances. However, there is definitely a problem here. I'm looking into it.

comment:2 by Russell Keith-Magee, 15 years ago

Resolution: fixed
Status: newclosed

(In [10787]) Fixed #11120 -- Corrected handling of inlines attached to inherited classes, broken by r10756. Thanks to George Song and Michael Strickland for the simultaneous reports.

comment:3 by Russell Keith-Magee, 15 years ago

(In [10788]) [1.0.X] Fixed #11120 -- Corrected handling of inlines attached to inherited classes, broken by r10756. Thanks to George Song and Michael Strickland for the simultaneous reports.

Merge of r10787 from trunk.

comment:4 by George Song, 15 years ago

Thanks for the quick fix on this, Russ. If you point me in the right place to put the test, I can try to write up a test for this case so it doesn't get broken again.

comment:5 by Alex Gaynor, 15 years ago

George, if you look at the commit Russ added tests for it :)

comment:6 by George Song, 15 years ago

Of course, the consummate professional he is.

comment:7 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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