Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12577. Reverted some changes from [12098] since [12206] made t…

…hem unneccessary. Also, added a test for using generic inlines with unique_together. Thanks for the report, Raffaele Salmaso.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12268 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 856a39e841080abc34e8ae3bb2b20f780c33762d 1 parent 63c5e66
Joseph Kocherhans authored January 21, 2010
24  django/contrib/contenttypes/generic.py
@@ -297,11 +297,7 @@ def __init__(self, data=None, files=None, instance=None, save_as_new=None,
297 297
         # Avoid a circular import.
298 298
         from django.contrib.contenttypes.models import ContentType
299 299
         opts = self.model._meta
300  
-        if instance is None:
301  
-            self.instance = self.model()
302  
-        else:
303  
-            self.instance = instance
304  
-        self.save_as_new = save_as_new
  300
+        self.instance = instance
305 301
         self.rel_name = '-'.join((
306 302
             opts.app_label, opts.object_name.lower(),
307 303
             self.ct_field.name, self.ct_fk_field.name,
@@ -328,19 +324,15 @@ def get_default_prefix(cls):
328 324
         ))
329 325
     get_default_prefix = classmethod(get_default_prefix)
330 326
 
331  
-    def _construct_form(self, i, **kwargs):
  327
+    def save_new(self, form, commit=True):
332 328
         # Avoid a circular import.
333 329
         from django.contrib.contenttypes.models import ContentType
334  
-        form = super(BaseGenericInlineFormSet, self)._construct_form(i, **kwargs)
335  
-        if self.save_as_new:
336  
-            # Remove the key from the form's data, we are only creating new instances.
337  
-            form.data[form.add_prefix(self.ct_fk_field.name)] = None
338  
-            form.data[form.add_prefix(self.ct_field.name)] = None
339  
-
340  
-        # Set the GenericForeignKey value here so that the form can do its validation.
341  
-        setattr(form.instance, self.ct_fk_field.attname, self.instance.pk)
342  
-        setattr(form.instance, self.ct_field.attname, ContentType.objects.get_for_model(self.instance).pk)
343  
-        return form
  330
+        kwargs = {
  331
+            self.ct_field.get_attname(): ContentType.objects.get_for_model(self.instance).pk,
  332
+            self.ct_fk_field.get_attname(): self.instance.pk,
  333
+        }
  334
+        new_obj = self.model(**kwargs)
  335
+        return save_instance(form, new_obj, commit=commit)
344 336
 
345 337
 def generic_inlineformset_factory(model, form=ModelForm,
346 338
                                   formset=BaseGenericInlineFormSet,
21  tests/regressiontests/generic_inline_admin/models.py
@@ -72,3 +72,24 @@ class MediaExcludeInline(generic.GenericTabularInline):
72 72
 
73 73
 admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
74 74
 
  75
+#
  76
+# Generic inline with unique_together
  77
+#
  78
+
  79
+class PhoneNumber(models.Model):
  80
+    content_type = models.ForeignKey(ContentType)
  81
+    object_id = models.PositiveIntegerField()
  82
+    content_object = generic.GenericForeignKey('content_type', 'object_id')
  83
+    phone_number = models.CharField(max_length=30)
  84
+
  85
+    class Meta:
  86
+        unique_together = (('content_type', 'object_id', 'phone_number',),)
  87
+
  88
+class Contact(models.Model):
  89
+    name = models.CharField(max_length=50)
  90
+    phone_numbers = generic.GenericRelation(PhoneNumber)
  91
+
  92
+class PhoneNumberInline(generic.GenericTabularInline):
  93
+    model = PhoneNumber
  94
+
  95
+admin.site.register(Contact, inlines=[PhoneNumberInline])
22  tests/regressiontests/generic_inline_admin/tests.py
@@ -176,3 +176,25 @@ def testExcludeParam(self):
176 176
         response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk)
177 177
         formset = response.context['inline_admin_formsets'][0].formset
178 178
         self.failIf('url' in formset.forms[0], 'The formset has excluded "url" field.')
  179
+
  180
+class GenericInlineAdminWithUniqueTogetherTest(TestCase):
  181
+    fixtures = ['users.xml']
  182
+
  183
+    def setUp(self):
  184
+        self.client.login(username='super', password='secret')
  185
+
  186
+    def tearDown(self):
  187
+        self.client.logout()
  188
+
  189
+    def testAdd(self):
  190
+        post_data = {
  191
+            "name": u"John Doe",
  192
+            # inline data
  193
+            "generic_inline_admin-phonenumber-content_type-object_id-TOTAL_FORMS": u"1",
  194
+            "generic_inline_admin-phonenumber-content_type-object_id-INITIAL_FORMS": u"0",
  195
+            "generic_inline_admin-phonenumber-content_type-object_id-0-id": "",
  196
+            "generic_inline_admin-phonenumber-content_type-object_id-0-phone_number": "555-555-5555",
  197
+        }
  198
+        response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/contact/add/')
  199
+        response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/contact/add/', post_data)
  200
+        self.failUnlessEqual(response.status_code, 302) # redirect somewhere

0 notes on commit 856a39e

Please sign in to comment.
Something went wrong with that request. Please try again.