summaryrefslogtreecommitdiff
path: root/tests/generic_relations_regress
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-11-12 21:35:52 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2013-11-16 20:07:35 +0200
commit0e079e4331a8be4dbd18d5e5776116330b0a5e61 (patch)
treef5547108801e6598768e530805fe19b18a5f580c /tests/generic_relations_regress
parentb642d540d4ef2e72b0a89fe95b8777702ce08973 (diff)
downloaddjango-0e079e4331a8be4dbd18d5e5776116330b0a5e61.tar.gz
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.
Diffstat (limited to 'tests/generic_relations_regress')
-rw-r--r--tests/generic_relations_regress/models.py12
-rw-r--r--tests/generic_relations_regress/tests.py11
2 files changed, 22 insertions, 1 deletions
diff --git a/tests/generic_relations_regress/models.py b/tests/generic_relations_regress/models.py
index 300ed9d54a..e9fb41dfe3 100644
--- a/tests/generic_relations_regress/models.py
+++ b/tests/generic_relations_regress/models.py
@@ -143,8 +143,18 @@ 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
diff --git a/tests/generic_relations_regress/tests.py b/tests/generic_relations_regress/tests.py
index 285bd43c47..6e6ada5872 100644
--- a/tests/generic_relations_regress/tests.py
+++ b/tests/generic_relations_regress/tests.py
@@ -1,6 +1,7 @@
from django.db.models import Q, Sum
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,
@@ -236,3 +237,13 @@ class GenericRelationTests(TestCase):
# Finally test that filtering works.
self.assertEqual(qs.filter(links__sum__isnull=True).count(), 1)
self.assertEqual(qs.filter(links__sum__isnull=False).count(), 0)
+
+ 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)