Fixed #342 -- added readonly_fields to ModelAdmin. Thanks Alex Gaynor for bootstrapping the patch.

ModelAdmin has been given a readonly_fields that allow field and calculated
values to be displayed alongside editable fields. This works on model
add/change pages and inlines.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11965 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2009-12-22 18:29:00 +00:00
parent 9233d04265
commit bcd9482a20
13 changed files with 504 additions and 162 deletions

View file

@ -172,6 +172,11 @@ The ``field_options`` dictionary can have the following keys:
'fields': (('first_name', 'last_name'), 'address', 'city', 'state'),
}
.. versionadded:: 1.2
``fields`` can contain values defined in
:attr:`ModelAdmin.readonly_fields` to be displayed as read-only.
* ``classes``
A list containing extra CSS classes to apply to the fieldset.
@ -210,6 +215,11 @@ the ``django.contrib.flatpages.FlatPage`` model as follows::
In the above example, only the fields 'url', 'title' and 'content' will be
displayed, sequentially, in the form.
.. versionadded:: 1.2
``fields`` can contain values defined in :attr:`ModelAdmin.readonly_fields`
to be displayed as read-only.
.. admonition:: Note
This ``fields`` option should not be confused with the ``fields``
@ -540,6 +550,21 @@ into a ``Input`` widget for either a ``ForeignKey`` or ``ManyToManyField``::
class ArticleAdmin(admin.ModelAdmin):
raw_id_fields = ("newspaper",)
.. attribute:: ModelAdmin.readonly_fields
.. versionadded:: 1.2
By default the admin shows all fields as editable. Any fields in this option
(which should be a ``list`` or ``tuple``) will display its data as-is and
non-editable. This option behaves nearly identical to :attr:`ModelAdmin.list_display`.
Usage is the same, however, when you specify :attr:`ModelAdmin.fields` or
:attr:`ModelAdmin.fieldsets` the read-only fields must be present to be shown
(they are ignored otherwise).
If ``readonly_fields`` is used without defining explicit ordering through
:attr:`ModelAdmin.fields` or :attr:`ModelAdmin.fieldsets` they will be added
last after all editable fields.
.. attribute:: ModelAdmin.save_as
Set ``save_as`` to enable a "save as" feature on admin change forms.
@ -744,6 +769,15 @@ model instance::
instance.save()
formset.save_m2m()
.. method:: ModelAdmin.get_readonly_fields(self, request, obj=None)
.. versionadded:: 1.2
The ``get_readonly_fields`` method is given the ``HttpRequest`` and the
``obj`` being edited (or ``None`` on an add form) and is expected to return a
``list`` or ``tuple`` of field names that will be displayed as read-only, as
described above in the :attr:`ModelAdmin.readonly_fields` section.
.. method:: ModelAdmin.get_urls(self)
.. versionadded:: 1.1