#11121 closed (duplicate)
Admin inlines break when the foreign key points to a model using MTI
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (2)
comment:1 by , 15 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Version: | 1.0 → SVN |
comment:2 by , 15 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|
Duplicate of #11120
Replying to Michael Strickland <moriogawa@gmail.com>: