Skip to content

Commit d1de759

Browse files
committed
Fixed django#19120 - Added an example of using ModelAdmin methods for read-only fields.
Thanks Daniele Procida for the patch.
1 parent 07361d1 commit d1de759

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

docs/ref/contrib/admin/index.txt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,15 +816,34 @@ subclass::
816816

817817
By default the admin shows all fields as editable. Any fields in this
818818
option (which should be a ``list`` or ``tuple``) will display its data
819-
as-is and non-editable. This option behaves nearly identical to
820-
:attr:`ModelAdmin.list_display`. Usage is the same, however, when you
821-
specify :attr:`ModelAdmin.fields` or :attr:`ModelAdmin.fieldsets` the
822-
read-only fields must be present to be shown (they are ignored otherwise).
819+
as-is and non-editable. Note that when specifying :attr:`ModelAdmin.fields`
820+
or :attr:`ModelAdmin.fieldsets` the read-only fields must be present to be
821+
shown (they are ignored otherwise).
823822

824823
If ``readonly_fields`` is used without defining explicit ordering through
825824
:attr:`ModelAdmin.fields` or :attr:`ModelAdmin.fieldsets` they will be
826825
added last after all editable fields.
827826

827+
A read-only field can not only display data from a model's field, it can
828+
also display the output of a a model's method or a method of the
829+
``ModelAdmin`` class itself. This is very similar to the way
830+
:attr:`ModelAdmin.list_display` behaves. This provides an easy way to use
831+
the admin interface to provide feedback on the status of the objects being
832+
edited, for example::
833+
834+
class PersonAdmin(ModelAdmin):
835+
readonly_fields = ('address_report',)
836+
837+
def address_report(self, instance):
838+
return ", ".join(instance.get_full_address()) or \
839+
"<span class='errors'>I can't determine this address.</span>"
840+
841+
# short_description functions like a model field's verbose_name
842+
address_report.short_description = "Address"
843+
# in this example, we have used HTML tags in the output
844+
address_report.allow_tags = True
845+
846+
828847
.. attribute:: ModelAdmin.save_as
829848

830849
Set ``save_as`` to enable a "save as" feature on admin change forms.

0 commit comments

Comments
 (0)