Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.5.X] Fixed #19120 - Added an example of using ModelAdmin methods f…

…or read-only fields.

Thanks Daniele Procida for the patch.

Backport of d1de759 from master
  • Loading branch information...
commit 75fb8f433fc0bc64fed0b8dcc540f7a3544f9aa4 1 parent 90af863
@timgraham timgraham authored
Showing with 23 additions and 4 deletions.
  1. +23 −4 docs/ref/contrib/admin/index.txt
View
27 docs/ref/contrib/admin/index.txt
@@ -816,15 +816,34 @@ subclass::
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).
+ as-is and non-editable. Note that when specifying :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.
+ 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
Set ``save_as`` to enable a "save as" feature on admin change forms.
Please sign in to comment.
Something went wrong with that request. Please try again.