0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

Fixed #19120 - Added an example of using ModelAdmin methods for read-only fields.

Thanks Daniele Procida for the patch.
This commit is contained in:
Tim Graham 2012-11-02 16:48:55 -04:00
parent 07361d1fd6
commit d1de7596b2

View File

@ -816,15 +816,34 @@ subclass::
By default the admin shows all fields as editable. Any fields in this 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 option (which should be a ``list`` or ``tuple``) will display its data
as-is and non-editable. This option behaves nearly identical to as-is and non-editable. Note that when specifying :attr:`ModelAdmin.fields`
:attr:`ModelAdmin.list_display`. Usage is the same, however, when you or :attr:`ModelAdmin.fieldsets` the read-only fields must be present to be
specify :attr:`ModelAdmin.fields` or :attr:`ModelAdmin.fieldsets` the shown (they are ignored otherwise).
read-only fields must be present to be shown (they are ignored otherwise).
If ``readonly_fields`` is used without defining explicit ordering through If ``readonly_fields`` is used without defining explicit ordering through
:attr:`ModelAdmin.fields` or :attr:`ModelAdmin.fieldsets` they will be :attr:`ModelAdmin.fields` or :attr:`ModelAdmin.fieldsets` they will be
added last after all editable fields. added last after all editable fields.
A read-only field can not only display data from a model's field, it can
also display the output of a a model's method or a method of the
``ModelAdmin`` class itself. This is very similar to the way
:attr:`ModelAdmin.list_display` behaves. This provides an easy way to use
the admin interface to provide feedback on the status of the objects being
edited, for example::
class PersonAdmin(ModelAdmin):
readonly_fields = ('address_report',)
def address_report(self, instance):
return ", ".join(instance.get_full_address()) or \
"<span class='errors'>I can't determine this address.</span>"
# short_description functions like a model field's verbose_name
address_report.short_description = "Address"
# in this example, we have used HTML tags in the output
address_report.allow_tags = True
.. attribute:: ModelAdmin.save_as .. attribute:: ModelAdmin.save_as
Set ``save_as`` to enable a "save as" feature on admin change forms. Set ``save_as`` to enable a "save as" feature on admin change forms.