﻿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
11121	Admin inlines break when the foreign key points to a model using MTI	Michael Strickland <moriogawa@…>	nobody	"Take a model (""Picture"" below) which includes a foreign key to another model (""Student""), which in turn uses MTI to inherit from a base model (""Person""):


{{{
class Person(models.Model):
    name = models.CharField(max_length=20)

class Student(Person):
    grad_year = models.IntegerField()

class Picture(models.Model):
    image = models.ImageField(upload_to='images')
    owner = models.ForeignKey(Student)
}}}


Previously, Picture could be included as an inline in Student's admin with no problems:


{{{
from example.models import Student, Picture
class PictureInline(admin.StackedInline):
    model = Picture

class StudentAdmin(admin.ModelAdmin):
    inlines = [PictureInline]
}}}



As of revision [10756], everything still works fine when dealing directly with the database - however, when ""Picture"" is included as an inline in StudentAdmin, a DoesNotExist exception is thrown when trying to add a new ""Student"" object (but '''not''' when editing an instance of Student that's already been saved):

http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/related.py#L243
{{{

 236. return getattr(instance, cache_name)
 237. except AttributeError:
 238. val = getattr(instance, self.field.attname)
 239. if val is None:
 240. # If NULL is an allowed value, return it.
 241. if self.field.null:
 242. return None
''' 243. raise self.field.rel.to.DoesNotExist ...'''
 244. other_field = self.field.rel.get_related_field()
 245. if other_field.rel:
 246. params = {'%s__pk' % self.field.rel.field_name: val}
 247. else:
 248. params = {'%s__exact' % self.field.rel.field_name: val}
}}}

A dirty fix that appears to work is to have the inline element's FK point to the parent model of the intended model (here, Person instead of Student). But that's not ideal.

The specifics of [10756] are over my head, but I'm assuming that this wasn't intended in the revision."		closed	Database layer (models, ORM)	dev		duplicate			Unreviewed	0	0	0	0	0	0
