1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

[1.6.x] Fixed #21428 -- editable GenericRelation regression

The GenericRelation refactoring removed GenericRelations from
model._meta.many_to_many. This had the side effect of disallowing
editable GenericRelations in ModelForms. Editable GenericRelations
aren't officially supported, but if we don't fix this we don't offer any
upgrade path for those who used the ability to set editable=True
in GenericRelation subclass.

Thanks to Trac alias joshcartme for the report and stephencmd and Loic
for working on this issue.

Backpatch of 0e079e4331 from master.
This commit is contained in:
Anssi Kääriäinen
2013-11-12 21:35:52 +02:00
parent e8dea1f35c
commit 1fd762c106
5 changed files with 34 additions and 6 deletions

View File

@@ -123,8 +123,17 @@ class Tag(models.Model):
class Board(models.Model):
name = models.CharField(primary_key=True, max_length=15)
class SpecialGenericRelation(generic.GenericRelation):
def __init__(self, *args, **kwargs):
super(SpecialGenericRelation, self).__init__(*args, **kwargs)
self.editable = True
self.save_form_data_calls = 0
def save_form_data(self, *args, **kwargs):
self.save_form_data_calls += 1
class HasLinks(models.Model):
links = generic.GenericRelation(Link)
links = SpecialGenericRelation(Link)
class Meta:
abstract = True

View File

@@ -1,6 +1,7 @@
from django.db.models import Q
from django.db.utils import IntegrityError
from django.test import TestCase, skipIfDBFeature
from django.forms.models import modelform_factory
from .models import (
Address, Place, Restaurant, Link, CharLink, TextLink,
@@ -212,3 +213,13 @@ class GenericRelationTests(TestCase):
# B would then fail).
self.assertNotIn(" join ", str(B.objects.exclude(a__flag=True).query).lower())
self.assertIn("content_type_id", str(B.objects.exclude(a__flag=True).query).lower())
def test_editable_generic_rel(self):
GenericRelationForm = modelform_factory(HasLinkThing, fields='__all__')
form = GenericRelationForm()
self.assertIn('links', form.fields)
form = GenericRelationForm({'links': None})
self.assertTrue(form.is_valid())
form.save()
links = HasLinkThing._meta.get_field_by_name('links')[0].field
self.assertEqual(links.save_form_data_calls, 1)