Code

Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#11120 closed (fixed)

Changeset 10756 breaks foreign key inline for multi-table subclasses

Reported by: gsong Owned by: nobody
Component: Database layer (models, ORM) Version: master
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: UI/UX:

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.

Attachments (0)

Change History (7)

comment:1 Changed 5 years ago by russellm

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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.

comment:2 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [10787]) Fixed #11120 -- Corrected handling of inlines attached to inherited classes, broken by r10756. Thanks to George Song and Michael Strickland for the simultaneous reports.

comment:3 Changed 5 years ago by russellm

(In [10788]) [1.0.X] Fixed #11120 -- Corrected handling of inlines attached to inherited classes, broken by r10756. Thanks to George Song and Michael Strickland for the simultaneous reports.

Merge of r10787 from trunk.

comment:4 Changed 5 years ago by gsong

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.

comment:5 Changed 5 years ago by Alex

George, if you look at the commit Russ added tests for it :)

comment:6 Changed 5 years ago by gsong

Of course, the consummate professional he is.

comment:7 Changed 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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.