Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9362 -- Prevented inline forms from overwriting the content_ty…

…pe_id attribute on objets being inlined. Thanks to carljm for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10667 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 80a54dd23bfda4bb297f9b13f2a54c1f10664cf5 1 parent 2c24bba
Russell Keith-Magee authored May 03, 2009
2  django/contrib/admin/helpers.py
@@ -137,7 +137,7 @@ def __init__(self, formset, form, fieldsets, prepopulated_fields, original):
137 137
         self.formset = formset
138 138
         self.original = original
139 139
         if original is not None:
140  
-            self.original.content_type_id = ContentType.objects.get_for_model(original).pk
  140
+            self.original_content_type_id = ContentType.objects.get_for_model(original).pk
141 141
         self.show_url = original and hasattr(original, 'get_absolute_url')
142 142
         super(InlineAdminForm, self).__init__(form, fieldsets, prepopulated_fields)
143 143
 
2  django/contrib/admin/templates/admin/edit_inline/stacked.html
@@ -10,7 +10,7 @@
10 10
     {% if inline_admin_formset.formset.can_delete and inline_admin_form.original %}<span class="delete">{{ inline_admin_form.deletion_field.field }} {{ inline_admin_form.deletion_field.label_tag }}</span>{% endif %}
11 11
   </h3>
12 12
   {% if inline_admin_form.show_url %}
13  
-  <p><a href="../../../r/{{ inline_admin_form.original.content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a></p>
  13
+  <p><a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a></p>
14 14
   {% endif %}
15 15
   {% if inline_admin_form.form.non_field_errors %}{{ inline_admin_form.form.non_field_errors }}{% endif %}
16 16
 
2  django/contrib/admin/templates/admin/edit_inline/tabular.html
@@ -24,7 +24,7 @@
24 24
         <td class="original">
25 25
           {% if inline_admin_form.original or inline_admin_form.show_url %}<p>
26 26
           {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
27  
-          {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original.content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
  27
+          {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
28 28
             </p>{% endif %}
29 29
           {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
30 30
           {{ inline_admin_form.fk_field.field }}
0  tests/regressiontests/admin_inlines/__init__.py
No changes.
51  tests/regressiontests/admin_inlines/models.py
... ...
@@ -0,0 +1,51 @@
  1
+"""
  2
+Testing of admin inline formsets.
  3
+
  4
+"""
  5
+from django.db import models
  6
+from django.contrib.contenttypes.models import ContentType
  7
+from django.contrib.contenttypes import generic
  8
+
  9
+class Parent(models.Model):
  10
+    name = models.CharField(max_length=50)
  11
+
  12
+    def __unicode__(self):
  13
+        return self.name
  14
+
  15
+class Teacher(models.Model):
  16
+    name = models.CharField(max_length=50)
  17
+
  18
+    def __unicode__(self):
  19
+        return self.name
  20
+
  21
+class Child(models.Model):
  22
+    name = models.CharField(max_length=50)
  23
+    teacher = models.ForeignKey(Teacher)
  24
+
  25
+    content_type = models.ForeignKey(ContentType)
  26
+    object_id = models.PositiveIntegerField()
  27
+    parent = generic.GenericForeignKey()
  28
+
  29
+    def __unicode__(self):
  30
+        return u'I am %s, a child of %s' % (self.name, self.parent)
  31
+
  32
+__test__ = {'API_TESTS': """
  33
+
  34
+# Regression test for #9362
  35
+
  36
+>>> sally = Teacher.objects.create(name='Sally')
  37
+>>> john = Parent.objects.create(name='John')
  38
+>>> joe = Child.objects.create(name='Joe', teacher=sally, parent=john)
  39
+
  40
+The problem depends only on InlineAdminForm and its "original" argument, so
  41
+we can safely set the other arguments to None/{}. We just need to check that
  42
+the content_type argument of Child isn't altered by the internals of the
  43
+inline form.
  44
+
  45
+>>> from django.contrib.admin.helpers import InlineAdminForm
  46
+>>> iaf = InlineAdminForm(None, None, {}, {}, joe)
  47
+>>> iaf.original
  48
+<Child: I am Joe, a child of John>
  49
+
  50
+"""
  51
+}

0 notes on commit 80a54dd

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