Ticket #15003: admin-doc-updates.diff
File admin-doc-updates.diff, 17.9 KB (added by , 14 years ago) |
---|
-
docs/ref/contrib/admin/index.txt
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 326bca4..5126036 100644
a b There are six steps in activating the Django admin site: 26 26 1. Add ``'django.contrib.admin'`` to your :setting:`INSTALLED_APPS` 27 27 setting. 28 28 29 2. Admin has two dependencies - ``django.contrib.auth`` and30 ``django.contrib.contenttypes``. If these applications are not29 2. Admin has two dependencies - :mod:`django.contrib.auth` and 30 :mod:`django.contrib.contenttypes`. If these applications are not 31 31 in your :setting:`INSTALLED_APPS` list, add them. 32 32 33 33 3. Determine which of your application's models should be editable in the … … Other topics 87 87 88 88 admin.site.register(Author) 89 89 90 ``ModelAdmin`` Options90 ``ModelAdmin`` options 91 91 ---------------------- 92 92 93 93 The ``ModelAdmin`` is very flexible. It has several options for dealing with … … subclass:: 97 97 class AuthorAdmin(admin.ModelAdmin): 98 98 date_hierarchy = 'pub_date' 99 99 100 .. attribute:: ModelAdmin.actions 101 102 A list of actions to make available on the change list page. See 103 :doc:`/ref/contrib/admin/actions` for details. 104 105 .. attribute:: ModelAdmin.actions_on_top 106 .. attribute:: ModelAdmin.actions_on_bottom 107 108 Controls where on the page the actions bar appears. By default, the admin 109 changelist displays actions at the top of the page (``actions_on_top = True; 110 actions_on_bottom = False``). 111 112 .. attribute:: ModelAdmin.actions_selection_counter 113 114 .. versionadded:: 1.2 115 116 Controls whether a selection counter is display next to the action dropdown. 117 By default, the admin changelist will display it 118 (``actions_selection_counter = True``). 119 100 120 .. attribute:: ModelAdmin.date_hierarchy 101 121 102 122 Set ``date_hierarchy`` to the name of a ``DateField`` or ``DateTimeField`` … … subclass:: 109 129 110 130 .. versionadded:: 1.3 111 131 112 113 114 132 This will intelligently populate itself based on available data, 133 e.g. if all the dates are in one month, it'll show the day-level 134 drill-down only. 115 135 116 .. attribute:: ModelAdmin. form136 .. attribute:: ModelAdmin.exclude 117 137 118 By default a ``ModelForm`` is dynamically created for your model. It is 119 used to create the form presented on both the add/change pages. You can 120 easily provide your own ``ModelForm`` to override any default form behavior 121 on the add/change pages. 138 This attribute, if given, should be a list of field names to exclude from 139 the form. 122 140 123 For an example see the section `Adding custom validation to the admin`_. 141 For example, let's consider the following model:: 142 143 class Author(models.Model): 144 name = models.CharField(max_length=100) 145 title = models.CharField(max_length=3) 146 birth_date = models.DateField(blank=True, null=True) 147 148 If you want a form for the ``Author`` model that includes only the ``name`` 149 and ``title`` fields, you would specify ``fields`` or ``exclude`` like 150 this:: 151 152 class AuthorAdmin(admin.ModelAdmin): 153 fields = ('name', 'title') 154 155 class AuthorAdmin(admin.ModelAdmin): 156 exclude = ('birth_date',) 157 158 Since the Author model only has three fields, ``name``, ``title``, and 159 ``birth_date``, the forms resulting from the above declarations will 160 contain exactly the same fields. 161 162 .. attribute:: ModelAdmin.fields 163 164 Use this option as an alternative to ``fieldsets`` if the layout does not 165 matter and if you want to only show a subset of the available fields in the 166 form. For example, you could define a simpler version of the admin form for 167 the ``django.contrib.flatpages.FlatPage`` model as follows:: 168 169 class FlatPageAdmin(admin.ModelAdmin): 170 fields = ('url', 'title', 'content') 171 172 In the above example, only the fields 'url', 'title' and 'content' will be 173 displayed, sequentially, in the form. 174 175 .. versionadded:: 1.2 176 177 ``fields`` can contain values defined in :attr:`ModelAdmin.readonly_fields` 178 to be displayed as read-only. 179 180 .. admonition:: Note 181 182 This ``fields`` option should not be confused with the ``fields`` 183 dictionary key that is within the ``fieldsets`` option, as described in 184 the previous section. 124 185 125 186 .. attribute:: ModelAdmin.fieldsets 126 187 … … subclass:: 207 268 ``django.utils.html.escape()`` to escape any HTML special 208 269 characters. 209 270 210 .. attribute:: ModelAdmin.fields 211 212 Use this option as an alternative to ``fieldsets`` if the layout does not 213 matter and if you want to only show a subset of the available fields in the 214 form. For example, you could define a simpler version of the admin form for 215 the ``django.contrib.flatpages.FlatPage`` model as follows:: 271 .. attribute:: ModelAdmin.filter_horizontal 216 272 217 class FlatPageAdmin(admin.ModelAdmin): 218 fields = ('url', 'title', 'content') 273 Use a nifty unobtrusive JavaScript "filter" interface instead of the 274 usability-challenged ``<select multiple>`` in the admin form. The value is 275 a list of fields that should be displayed as a horizontal filter interface. 276 See ``filter_vertical`` to use a vertical interface. 219 277 220 In the above example, only the fields 'url', 'title' and 'content' will be 221 displayed, sequentially, in the form. 278 .. attribute:: ModelAdmin.filter_vertical 222 279 223 .. versionadded:: 1.2 280 Same as ``filter_horizontal``, but is a vertical display of the filter 281 interface. 224 282 225 ``fields`` can contain values defined in :attr:`ModelAdmin.readonly_fields` 226 to be displayed as read-only. 283 .. attribute:: ModelAdmin.form 227 284 228 .. admonition:: Note 285 By default a ``ModelForm`` is dynamically created for your model. It is 286 used to create the form presented on both the add/change pages. You can 287 easily provide your own ``ModelForm`` to override any default form behavior 288 on the add/change pages. 229 289 230 This ``fields`` option should not be confused with the ``fields`` 231 dictionary key that is within the ``fieldsets`` option, as described in 232 the previous section. 290 For an example see the section `Adding custom validation to the admin`_. 233 291 234 .. attribute:: ModelAdmin. exclude292 .. attribute:: ModelAdmin.formfield_overrides 235 293 236 This attribute, if given, should be a list of field names to exclude from 237 the form. 294 This provides a quick-and-dirty way to override some of the 295 :class:`~django.forms.Field` options for use in the admin. 296 ``formfield_overrides`` is a dictionary mapping a field class to a dict of 297 arguments to pass to the field at construction time. 238 298 239 For example, let's consider the following model:: 299 Since that's a bit abstract, let's look at a concrete example. The most 300 common use of ``formfield_overrides`` is to add a custom widget for a 301 certain type of field. So, imagine we've written a ``RichTextEditorWidget`` 302 that we'd like to use for large text fields instead of the default 303 ``<textarea>``. Here's how we'd do that:: 240 304 241 class Author(models.Model): 242 name = models.CharField(max_length=100) 243 title = models.CharField(max_length=3) 244 birth_date = models.DateField(blank=True, null=True) 305 from django.db import models 306 from django.contrib import admin 245 307 246 If you want a form for the ``Author`` model that includes only the ``name``247 and ``title`` fields, you would specify ``fields`` or ``exclude`` like248 this::308 # Import our custom widget and our model from where they're defined 309 from myapp.widgets import RichTextEditorWidget 310 from myapp.models import MyModel 249 311 250 class AuthorAdmin(admin.ModelAdmin): 251 fields = ('name', 'title') 312 class MyModelAdmin(admin.ModelAdmin): 313 formfield_overrides = { 314 models.TextField: {'widget': RichTextEditorWidget}, 315 } 252 316 253 class AuthorAdmin(admin.ModelAdmin): 254 exclude = ('birth_date',) 317 Note that the key in the dictionary is the actual field class, *not* a 318 string. The value is another dictionary; these arguments will be passed to 319 :meth:`~django.forms.Field.__init__`. See :doc:`/ref/forms/api` for 320 details. 255 321 256 Since the Author model only has three fields, ``name``, ``title``, and 257 ``birth_date``, the forms resulting from the above declarations will 258 contain exactly the same fields. 322 .. warning:: 259 323 260 .. attribute:: ModelAdmin.filter_horizontal 324 If you want to use a custom widget with a relation field (i.e. 325 :class:`~django.db.models.ForeignKey` or 326 :class:`~django.db.models.ManyToManyField`), make sure you haven't 327 included that field's name in ``raw_id_fields`` or ``radio_fields``. 261 328 262 Use a nifty unobtrusive JavaScript "filter" interface instead of the263 usability-challenged ``<select multiple>`` in the admin form. The value is264 a list of fields that should be displayed as a horizontal filter interface.265 See ``filter_vertical`` to use a vertical interface.329 ``formfield_overrides`` won't let you change the widget on relation 330 fields that have ``raw_id_fields`` or ``radio_fields`` set. That's 331 because ``raw_id_fields`` and ``radio_fields`` imply custom widgets of 332 their own. 266 333 267 .. attribute:: ModelAdmin. filter_vertical334 .. attribute:: ModelAdmin.inlines 268 335 269 Same as ``filter_horizontal``, but is a vertical display of the filter 270 interface. 336 See :class:`InlineModelAdmin` objects below. 271 337 272 338 .. attribute:: ModelAdmin.list_display 273 339 … … subclass:: 496 562 regardless of this setting if one of the ``list_display`` fields is a 497 563 ``ForeignKey``. 498 564 499 .. attribute:: ModelAdmin.inlines500 501 See :class:`InlineModelAdmin` objects below.502 503 565 .. attribute:: ModelAdmin.ordering 504 566 505 567 Set ``ordering`` to specify how lists of objects should be ordered in the 506 568 Django admin views. This should be a list or tuple in the same format as a 507 model's ``ordering`` parameter.569 model's :attr:`~django.db.models.Options.ordering` parameter. 508 570 509 571 If this isn't provided, the Django admin will use the model's default 510 572 ordering. … … subclass:: 674 736 Performs a full-text match. This is like the default search method but 675 737 uses an index. Currently this is only available for MySQL. 676 738 677 .. attribute:: ModelAdmin.formfield_overrides678 679 This provides a quick-and-dirty way to override some of the680 :class:`~django.forms.Field` options for use in the admin.681 ``formfield_overrides`` is a dictionary mapping a field class to a dict of682 arguments to pass to the field at construction time.683 684 Since that's a bit abstract, let's look at a concrete example. The most685 common use of ``formfield_overrides`` is to add a custom widget for a686 certain type of field. So, imagine we've written a ``RichTextEditorWidget``687 that we'd like to use for large text fields instead of the default688 ``<textarea>``. Here's how we'd do that::689 690 from django.db import models691 from django.contrib import admin692 693 # Import our custom widget and our model from where they're defined694 from myapp.widgets import RichTextEditorWidget695 from myapp.models import MyModel696 697 class MyModelAdmin(admin.ModelAdmin):698 formfield_overrides = {699 models.TextField: {'widget': RichTextEditorWidget},700 }701 702 Note that the key in the dictionary is the actual field class, *not* a703 string. The value is another dictionary; these arguments will be passed to704 :meth:`~django.forms.Field.__init__`. See :doc:`/ref/forms/api` for705 details.706 707 .. warning::708 709 If you want to use a custom widget with a relation field (i.e.710 :class:`~django.db.models.ForeignKey` or711 :class:`~django.db.models.ManyToManyField`), make sure you haven't712 included that field's name in ``raw_id_fields`` or ``radio_fields``.713 714 ``formfield_overrides`` won't let you change the widget on relation715 fields that have ``raw_id_fields`` or ``radio_fields`` set. That's716 because ``raw_id_fields`` and ``radio_fields`` imply custom widgets of717 their own.718 719 .. attribute:: ModelAdmin.actions720 721 A list of actions to make available on the change list page. See722 :doc:`/ref/contrib/admin/actions` for details.723 724 .. attribute:: ModelAdmin.actions_on_top725 .. attribute:: ModelAdmin.actions_on_bottom726 727 Controls where on the page the actions bar appears. By default, the admin728 changelist displays actions at the top of the page (``actions_on_top = True;729 actions_on_bottom = False``).730 731 .. attribute:: ModelAdmin.actions_selection_counter732 733 .. versionadded:: 1.2734 735 Controls whether a selection counter is display next to the action dropdown.736 By default, the admin changelist will display it737 (``actions_selection_counter = True``).738 739 739 Custom template options 740 740 ~~~~~~~~~~~~~~~~~~~~~~~ 741 741 … … on your ``ModelAdmin``:: 1043 1043 } 1044 1044 js = ("my_code.js",) 1045 1045 1046 Keep in mind that this will be prepended with ``MEDIA_URL``. The same rules1047 apply as :doc:`regular media definitions on forms </topics/forms/media>`.1046 Keep in mind that this will be prepended with :setting:`MEDIA_URL`. The same 1047 rules apply as :doc:`regular media definitions on forms </topics/forms/media>`. 1048 1048 1049 1049 Django admin Javascript makes use of the `jQuery`_ library. To avoid 1050 1050 conflict with user scripts, Django's jQuery is namespaced as … … automatically:: 1255 1255 FriendshipInline, 1256 1256 ] 1257 1257 1258 Working with Many-to-Many Models1258 Working with many-to-many models 1259 1259 -------------------------------- 1260 1260 1261 1261 .. versionadded:: 1.2 1262 1262 1263 1263 By default, admin widgets for many-to-many relations will be displayed 1264 on whichever model contains the actual reference to the ``ManyToManyField``. 1265 Depending on your ``ModelAdmin`` definition, each many-to-many field in your 1266 model will be represented by a standard HTML ``<select multiple>``, a 1267 horizontal or vertical filter, or a ``raw_id_admin`` widget. However, it is 1268 also possible to to replace these widgets with inlines. 1264 on whichever model contains the actual reference to the 1265 :class:`~django.db.models.ManyToManyField`. Depending on your ``ModelAdmin`` 1266 definition, each many-to-many field in your model will be represented by a 1267 standard HTML ``<select multiple>``, a horizontal or vertical filter, or a 1268 ``raw_id_admin`` widget. However, it is also possible to to replace these 1269 widgets with inlines. 1269 1270 1270 1271 Suppose we have the following models:: 1271 1272 … … In all other respects, the ``InlineModelAdmin`` is exactly the same as any 1311 1312 other. You can customize the appearance using any of the normal 1312 1313 ``ModelAdmin`` properties. 1313 1314 1314 Working with Many-to-Many Intermediary Models1315 --------------------------------------------- -1315 Working with many-to-many intermediary models 1316 --------------------------------------------- 1316 1317 1317 1318 When you specify an intermediary model using the ``through`` argument to a 1318 ``ManyToManyField``, the admin will not display a widget by default. This is 1319 because each instance of that intermediary model requires more information 1320 than could be displayed in a single widget, and the layout required for 1321 multiple widgets will vary depending on the intermediate model. 1319 :class:`~django.db.models.ManyToManyField`, the admin will not display a 1320 widget by default. This is because each instance of that intermediary model 1321 requires more information than could be displayed in a single widget, and the 1322 layout required for multiple widgets will vary depending on the intermediate 1323 model. 1322 1324 1323 1325 However, we still want to be able to edit that information inline. Fortunately, 1324 1326 this is easy to do with inline admin models. Suppose we have the following … … other inline. In your ``admin.py`` for this example app:: 1407 1409 See the :doc:`contenttypes documentation </ref/contrib/contenttypes>` for more 1408 1410 specific information. 1409 1411 1410 Overriding Admin Templates1412 Overriding admin templates 1411 1413 ========================== 1412 1414 1413 1415 It is relatively easy to override many of the templates which the admin module … … directory. 1422 1424 1423 1425 In order to override one or more of them, first create an ``admin`` directory 1424 1426 in your project's ``templates`` directory. This can be any of the directories 1425 you specified in ``TEMPLATE_DIRS``.1427 you specified in :setting:`TEMPLATE_DIRS`. 1426 1428 1427 1429 Within this ``admin`` directory, create sub-directories named after your app. 1428 1430 Within these app subdirectories create sub-directories named after your models. … … Templates can override or extend base admin templates as described in 1548 1550 1549 1551 Path to a custom template that will be used by the admin site login view. 1550 1552 1551 .. versionadded:: 1.31552 1553 1553 .. attribute:: AdminSite.login_form 1554 1554 1555 .. versionadded:: 1.3 1556 1555 1557 Subclass of :class:`~django.contrib.auth.forms.AuthenticationForm` that 1556 1558 will be used by the admin site login view. 1557 1559 … … a pattern for your new view. 1652 1654 .. note:: 1653 1655 Any view you render that uses the admin templates, or extends the base 1654 1656 admin template, should provide the ``current_app`` argument to 1655 ``RequestContext`` or ``Context`` when rendering the template. It should 1656 be set to either ``self.name`` if your view is on an ``AdminSite`` or 1657 ``self.admin_site.name`` if your view is on a ``ModelAdmin``. 1657 :class:`~django.template.RequestContext` or :class:`~django.template.Context` 1658 when rendering the template. It should be set to either ``self.name`` if 1659 your view is on an ``AdminSite`` or ``self.admin_site.name`` if your view 1660 is on a ``ModelAdmin``. 1658 1661 1659 1662 .. _admin-reverse-urls: 1660 1663 1661 Reversing Admin URLs1664 Reversing admin URLs 1662 1665 ==================== 1663 1666 1664 1667 When an :class:`AdminSite` is deployed, the views provided by that site are