Code

Ticket #2259: 2259-poc.diff

File 2259-poc.diff, 2.9 KB (added by bouke, 8 months ago)
Line 
1diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
2index 6ab842d..d668e05 100644
3--- a/django/contrib/admin/options.py
4+++ b/django/contrib/admin/options.py
5@@ -21,7 +21,8 @@ from django.core.urlresolvers import reverse
6 from django.db import models, transaction, router
7 from django.db.models.constants import LOOKUP_SEP
8 from django.db.models.related import RelatedObject
9-from django.db.models.fields import BLANK_CHOICE_DASH, FieldDoesNotExist
10+from django.db.models.fields import (BLANK_CHOICE_DASH, FieldDoesNotExist,
11+                                     AutoField)
12 from django.db.models.sql.constants import QUERY_TERMS
13 from django.forms.formsets import all_valid, DELETION_FIELD_NAME
14 from django.forms.models import (modelform_factory, modelformset_factory,
15@@ -96,6 +97,7 @@ class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)):
16     radio_fields = {}
17     prepopulated_fields = {}
18     formfield_overrides = {}
19+    auto_pk_readonly_field = True
20     readonly_fields = ()
21     ordering = None
22     view_on_site = True
23@@ -311,7 +313,11 @@ class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)):
24         """
25         Hook for specifying custom readonly fields.
26         """
27-        return self.readonly_fields
28+        if (self.auto_pk_readonly_field and obj is not None
29+                and not isinstance(self.opts.pk, AutoField)):
30+            return self.readonly_fields + (self.opts.pk.attname,)
31+        else:
32+            return self.readonly_fields
33 
34     def get_prepopulated_fields(self, request, obj=None):
35         """
36diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
37index 1301437..c23c794 100644
38--- a/docs/ref/contrib/admin/index.txt
39+++ b/docs/ref/contrib/admin/index.txt
40@@ -159,6 +159,15 @@ subclass::
41     By default, the admin changelist will display it
42     (``actions_selection_counter = True``).
43 
44+.. attribute:: ModelAdmin.auto_pk_readonly_field
45+
46+    .. versionadded:: 1.7
47+
48+        If the primary key is not an ``AutoField``, this controls whether the
49+        primary key field is made read-only for existing objects. If set to
50+        ``False``, the change form reverts to pre-1.7 behaviour and the primary
51+        key field will remain editable.
52+
53 .. attribute:: ModelAdmin.date_hierarchy
54 
55     Set ``date_hierarchy`` to the name of a ``DateField`` or ``DateTimeField``
56diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt
57index 2e596b3..2893f74 100644
58--- a/docs/releases/1.7.txt
59+++ b/docs/releases/1.7.txt
60@@ -172,6 +172,11 @@ Minor features
61   <django.contrib.admin.ModelAdmin.view_on_site>` to control whether or not to
62   display the "View on site" link.
63 
64+* Primary key fields will now be read-only for change forms by default, but
65+  can be made editable again by setting
66+  :attr:`ModelAdmin.auto_pk_readonly_field<django.contrib.admin.ModelAdmin.auto_pk_readonly_field>`
67+  to ``False``.
68+
69 :mod:`django.contrib.auth`
70 ^^^^^^^^^^^^^^^^^^^^^^^^^^
71