Fixed #26524 -- Fixed crash in admin change view when displaying many to many forward refs.

Thanks Tim Graham for the regression test.
This commit is contained in:
Jon Dufresne 2016-09-11 08:19:56 -07:00
parent f0f3de3c96
commit e24c0a2d7c
3 changed files with 13 additions and 1 deletions

View File

@ -317,7 +317,7 @@ def _get_non_gfk_field(opts, name):
raise FieldDoesNotExist()
# Avoid coercing <FK>_id fields to FK
if field.is_relation and hasattr(field, 'attname') and field.attname == name:
if field.is_relation and not field.many_to_many and hasattr(field, 'attname') and field.attname == name:
raise FieldIsAForeignKeyColumnName()
return field

View File

@ -596,9 +596,13 @@ class CyclicTwo(models.Model):
return self.name
@python_2_unicode_compatible
class Topping(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class Pizza(models.Model):
name = models.CharField(max_length=20)

View File

@ -4696,6 +4696,14 @@ class ReadonlyTest(AdminFieldExtractionMixin, TestCase):
response = self.client.get(reverse('admin:admin_views_topping_add'))
self.assertEqual(response.status_code, 200)
def test_readonly_manytomany_forwards_ref(self):
topping = Topping.objects.create(name='Salami')
pizza = Pizza.objects.create(name='Americano')
pizza.toppings.add(topping)
response = self.client.get(reverse('admin:admin_views_pizza_change', args=(pizza.pk,)))
self.assertContains(response, '<label>Toppings:</label>', html=True)
self.assertContains(response, '<p>Salami</p>', html=True)
def test_readonly_onetoone_backwards_ref(self):
"""
Can reference a reverse OneToOneField in ModelAdmin.readonly_fields.