﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
11120	Changeset 10756 breaks foreign key inline for multi-table subclasses	George Song	nobody	"In [10756] `BaseInlineFormSet.add_fields()` added [/browser/django/trunk/django/forms/models.py?rev=10756#L756 line 756]:
{{{
#!python
to_field=self.fk.rel.field_name,
}}}
This causes ForeignKey inlines for multi-table subclasses to break in the Admin.

Test `models.py`:
{{{
#!python
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`:
{{{
#!python
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."		closed	Database layer (models, ORM)	dev		fixed	multi table inheritance foreignkey inline		Accepted	0	0	0	0	0	0
