#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 , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:3 by , 16 years ago
comment:4 by , 16 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.
Note:
See TracTickets
for help on using tickets.
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.