Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #13696 -- ensured inline pk field is rendered

Backport of 3aad955 from master.
  • Loading branch information...
commit 706e542eb5db0d14277f34566d351b875e645bac 1 parent 9d3f7a2
Karen Tracey authored July 21, 2013
8  django/contrib/admin/helpers.py
@@ -266,10 +266,12 @@ def __iter__(self):
266 266
             yield InlineFieldset(self.formset, self.form, name,
267 267
                 self.readonly_fields, model_admin=self.model_admin, **options)
268 268
 
269  
-    def has_auto_field(self):
270  
-        if self.form._meta.model._meta.has_auto_field:
  269
+    def needs_explicit_pk_field(self):
  270
+        # Auto fields are editable (oddly), so need to check for auto or non-editable pk
  271
+        if self.form._meta.model._meta.has_auto_field or not self.form._meta.model._meta.pk.editable:
271 272
             return True
272  
-        # Also search any parents for an auto field.
  273
+        # Also search any parents for an auto field. (The pk info is propagated to child
  274
+        # models so that does not need to be checked in parents.)
273 275
         for parent in self.form._meta.model._meta.get_parent_list():
274 276
             if parent._meta.has_auto_field:
275 277
                 return True
2  django/contrib/admin/templates/admin/edit_inline/stacked.html
@@ -13,7 +13,7 @@
13 13
   {% for fieldset in inline_admin_form %}
14 14
     {% include "admin/includes/fieldset.html" %}
15 15
   {% endfor %}
16  
-  {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
  16
+  {% if inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
17 17
   {{ inline_admin_form.fk_field.field }}
18 18
 </div>{% endfor %}
19 19
 </div>
2  django/contrib/admin/templates/admin/edit_inline/tabular.html
@@ -29,7 +29,7 @@
29 29
           {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
30 30
           {% if inline_admin_form.show_url %}<a href="{% url 'admin:view_on_site' inline_admin_form.original_content_type_id inline_admin_form.original.pk %}">{% trans "View on site" %}</a>{% endif %}
31 31
             </p>{% endif %}
32  
-          {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
  32
+          {% if inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
33 33
           {{ inline_admin_form.fk_field.field }}
34 34
           {% spaceless %}
35 35
           {% for fieldset in inline_admin_form %}
20  tests/admin_inlines/admin.py
@@ -12,8 +12,26 @@ class BookInline(admin.TabularInline):
12 12
     model = Author.books.through
13 13
 
14 14
 
  15
+class NonAutoPKBookTabularInline(admin.TabularInline):
  16
+    model = NonAutoPKBook
  17
+
  18
+
  19
+class NonAutoPKBookStackedInline(admin.StackedInline):
  20
+    model = NonAutoPKBook
  21
+
  22
+
  23
+class EditablePKBookTabularInline(admin.TabularInline):
  24
+    model = EditablePKBook
  25
+
  26
+
  27
+class EditablePKBookStackedInline(admin.StackedInline):
  28
+    model = EditablePKBook
  29
+
  30
+
15 31
 class AuthorAdmin(admin.ModelAdmin):
16  
-    inlines = [BookInline]
  32
+    inlines = [BookInline,
  33
+        NonAutoPKBookTabularInline, NonAutoPKBookStackedInline,
  34
+        EditablePKBookTabularInline, EditablePKBookStackedInline]
17 35
 
18 36
 
19 37
 class InnerInline(admin.StackedInline):
20  tests/admin_inlines/models.py
@@ -3,6 +3,7 @@
3 3
 
4 4
 """
5 5
 from __future__ import unicode_literals
  6
+import random
6 7
 
7 8
 from django.db import models
8 9
 from django.contrib.contenttypes.models import ContentType
@@ -48,6 +49,25 @@ class Author(models.Model):
48 49
     books = models.ManyToManyField(Book)
49 50
 
50 51
 
  52
+class NonAutoPKBook(models.Model):
  53
+    rand_pk = models.IntegerField(primary_key=True, editable=False)
  54
+    author = models.ForeignKey(Author)
  55
+    title = models.CharField(max_length=50)
  56
+
  57
+    def save(self, *args, **kwargs):
  58
+        while not self.rand_pk:
  59
+            test_pk = random.randint(1, 99999)
  60
+            if not NonAutoPKBook.objects.filter(rand_pk=test_pk).exists():
  61
+                self.rand_pk = test_pk
  62
+        super(NonAutoPKBook, self).save(*args, **kwargs)
  63
+
  64
+
  65
+class EditablePKBook(models.Model):
  66
+    manual_pk = models.IntegerField(primary_key=True)
  67
+    author = models.ForeignKey(Author)
  68
+    title = models.CharField(max_length=50)
  69
+
  70
+
51 71
 class Holder(models.Model):
52 72
     dummy = models.IntegerField()
53 73
 
18  tests/admin_inlines/tests.py
@@ -211,6 +211,24 @@ def test_custom_get_extra_form(self):
211 211
         self.assertContains(response, max_forms_input % 2)
212 212
         self.assertContains(response, total_forms_hidden)
213 213
 
  214
+    def test_inline_nonauto_noneditable_pk(self):
  215
+        response = self.client.get('/admin/admin_inlines/author/add/')
  216
+        self.assertContains(response,
  217
+            '<input id="id_nonautopkbook_set-0-rand_pk" name="nonautopkbook_set-0-rand_pk" type="hidden" />',
  218
+             html=True)
  219
+        self.assertContains(response,
  220
+            '<input id="id_nonautopkbook_set-2-0-rand_pk" name="nonautopkbook_set-2-0-rand_pk" type="hidden" />',
  221
+             html=True)
  222
+
  223
+    def test_inline_editable_pk(self):
  224
+        response = self.client.get('/admin/admin_inlines/author/add/')
  225
+        self.assertContains(response,
  226
+            '<input class="vIntegerField" id="id_editablepkbook_set-0-manual_pk" name="editablepkbook_set-0-manual_pk" type="text" />',
  227
+             html=True, count=1)
  228
+        self.assertContains(response,
  229
+            '<input class="vIntegerField" id="id_editablepkbook_set-2-0-manual_pk" name="editablepkbook_set-2-0-manual_pk" type="text" />',
  230
+             html=True, count=1)
  231
+
214 232
 
215 233
 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
216 234
 class TestInlineMedia(TestCase):

0 notes on commit 706e542

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