Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#11121 closed (duplicate)

Admin inlines break when the foreign key points to a model using MTI

Reported by: Michael Strickland <moriogawa@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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.

Attachments (0)

Change History (2)

comment:1 in reply to: ↑ description Changed 5 years ago by Michael Strickland <moriogawa@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version changed from 1.0 to SVN

Replying to Michael Strickland <moriogawa@gmail.com>:

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.

comment:2 Changed 5 years ago by gsong

  • Component changed from Uncategorized to Database layer (models, ORM)

Duplicate of #11120

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.