~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to docs/ref/contrib/admin/index.txt

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-10-12 11:34:35 UTC
  • mfrom: (4.4.9 sid)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: james.westby@ubuntu.com-20101012113435-5rk3p18nyanuhj6g
* SECURITY UPDATE: XSS in CSRF protections. New upstream release
  - CVE-2010-3082
* debian/patches/01_disable_url_verify_regression_tests.diff:
  - updated to disable another test that fails without internet connection
  - patch based on work by Kai Kasurinen and Krzysztof Klimonda
* debian/control: don't Build-Depends on locales-all, which doesn't exist
  in maverick

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
.. _ref-contrib-admin:
2
 
 
3
1
=====================
4
2
The Django admin site
5
3
=====================
7
5
.. module:: django.contrib.admin
8
6
   :synopsis: Django's admin site.
9
7
 
10
 
.. currentmodule:: django.contrib.admin
11
 
 
12
8
One of the most powerful parts of Django is the automatic admin interface. It
13
9
reads metadata in your model to provide a powerful and production-ready
14
10
interface that content producers can immediately use to start adding content to
474
470
 
475
471
.. attribute:: ModelAdmin.list_select_related
476
472
 
477
 
Set ``list_select_related`` to tell Django to use ``select_related()`` in
478
 
retrieving the list of objects on the admin change list page. This can save you
479
 
a bunch of database queries.
 
473
Set ``list_select_related`` to tell Django to use
 
474
:meth:`~django.db.models.QuerySet.select_related` in retrieving the list of
 
475
objects on the admin change list page. This can save you a bunch of database
 
476
queries.
480
477
 
481
478
The value should be either ``True`` or ``False``. Default is ``False``.
482
479
 
483
 
Note that Django will use ``select_related()``, regardless of this setting,
484
 
if one of the ``list_display`` fields is a ``ForeignKey``.
485
 
 
486
 
For more on ``select_related()``, see
487
 
:ref:`the select_related() docs <select-related>`.
 
480
Note that Django will use :meth:`~django.db.models.QuerySet.select_related`,
 
481
regardless of this setting, if one of the ``list_display`` fields is a
 
482
``ForeignKey``.
488
483
 
489
484
.. attribute:: ModelAdmin.inlines
490
485
 
595
590
somebody submits a search query in that text box.
596
591
 
597
592
These fields should be some kind of text field, such as ``CharField`` or
598
 
``TextField``. You can also perform a related lookup on a ``ForeignKey`` with
599
 
the lookup API "follow" notation::
 
593
``TextField``. You can also perform a related lookup on a ``ForeignKey`` or
 
594
``ManyToManyField`` with the lookup API "follow" notation::
600
595
 
601
596
    search_fields = ['foreign_key__related_fieldname']
602
597
 
 
598
For example, if you have a blog entry with an author, the following definition
 
599
would enable search blog entries by the email address of the author::
 
600
 
 
601
    search_fields = ['user__email']
 
602
 
603
603
When somebody does a search in the admin search box, Django splits the search
604
604
query into words and returns all objects that contain each of the words, case
605
605
insensitive, where each word must be in at least one of ``search_fields``. For
674
674
 
675
675
Note that the key in the dictionary is the actual field class, *not* a string.
676
676
The value is another dictionary; these arguments will be passed to
677
 
:meth:`~django.forms.Field.__init__`. See :ref:`ref-forms-api` for details.
 
677
:meth:`~django.forms.Field.__init__`. See :doc:`/ref/forms/api` for details.
678
678
 
679
679
.. warning::
680
680
 
692
692
.. versionadded:: 1.1
693
693
 
694
694
A list of actions to make available on the change list page. See
695
 
:ref:`ref-contrib-admin-actions` for details.
 
695
:doc:`/ref/contrib/admin/actions` for details.
696
696
 
697
697
.. attribute:: ModelAdmin.actions_on_top
698
698
.. attribute:: ModelAdmin.actions_on_bottom
743
743
 
744
744
    Path to a custom template, used by the :meth:`delete_selected`
745
745
    action method for displaying a confirmation page when deleting one
746
 
    or more objects. See the :ref:`actions
747
 
    documentation<ref-contrib-admin-actions>`.
 
746
    or more objects. See the :doc:`actions
 
747
    documentation</ref/contrib/admin/actions>`.
748
748
 
749
749
.. attribute:: ModelAdmin.object_history_template
750
750
 
801
801
 
802
802
The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for
803
803
that ModelAdmin in the same way as a URLconf.  Therefore you can extend them as
804
 
documented in :ref:`topics-http-urls`::
 
804
documented in :doc:`/topics/http/urls`::
805
805
 
806
806
    class MyModelAdmin(admin.ModelAdmin):
807
807
        def get_urls(self):
829
829
 
830
830
Since this is usually not what you want, Django provides a convenience wrapper
831
831
to check permissions and mark the view as non-cacheable. This wrapper is
832
 
:meth:`AdminSite.admin_view` (i.e.  ``self.admin_site.admin_view`` inside a
 
832
:meth:`AdminSite.admin_view` (i.e. ``self.admin_site.admin_view`` inside a
833
833
``ModelAdmin`` instance); use it like so::
834
834
 
835
835
    class MyModelAdmin(admin.ModelAdmin):
865
865
        def formfield_for_foreignkey(self, db_field, request, **kwargs):
866
866
            if db_field.name == "car":
867
867
                kwargs["queryset"] = Car.objects.filter(owner=request.user)
868
 
                return db_field.formfield(**kwargs)
869
868
            return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
870
869
 
871
870
This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key field
872
 
to only the cars owned by the ``User`` instance.
 
871
to only display the cars owned by the ``User`` instance.
 
872
 
 
873
.. method:: ModelAdmin.formfield_for_manytomany(self, db_field, request, **kwargs)
 
874
 
 
875
.. versionadded:: 1.1
 
876
 
 
877
Like the ``formfield_for_foreignkey`` method, the ``formfield_for_manytomany``
 
878
method can be overridden to change the default formfield for a many to many
 
879
field. For example, if an owner can own multiple cars and cars can belong
 
880
to multiple owners -- a many to many relationship -- you could filter the
 
881
``Car`` foreign key field to only display the cars owned by the ``User``::
 
882
 
 
883
    class MyModelAdmin(admin.ModelAdmin):
 
884
        def formfield_for_manytomany(self, db_field, request, **kwargs):
 
885
            if db_field.name == "cars":
 
886
                kwargs["queryset"] = Car.objects.filter(owner=request.user)
 
887
            return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
873
888
 
874
889
.. method:: ModelAdmin.queryset(self, request)
875
890
 
950
965
            js = ("my_code.js",)
951
966
 
952
967
Keep in mind that this will be prepended with ``MEDIA_URL``. The same rules
953
 
apply as :ref:`regular media definitions on forms <topics-forms-media>`.
 
968
apply as :doc:`regular media definitions on forms </topics/forms/media>`.
954
969
 
955
970
Django admin Javascript makes use of the `jQuery`_ library. To avoid
956
971
conflict with user scripts, Django's jQuery is namespaced as
983
998
            return self.cleaned_data["name"]
984
999
 
985
1000
It is important you use a ``ModelForm`` here otherwise things can break. See the
986
 
:ref:`forms <ref-forms-index>` documentation on :ref:`custom validation
987
 
<ref-forms-validation>` and, more specifically, the
 
1001
:doc:`forms </ref/forms/index>` documentation on :doc:`custom validation
 
1002
</ref/forms/validation>` and, more specifically, the
988
1003
:ref:`model form validation notes <overriding-modelform-clean-method>` for more
989
1004
information.
990
1005
 
993
1008
``InlineModelAdmin`` objects
994
1009
============================
995
1010
 
 
1011
.. class:: InlineModelAdmin
 
1012
 
996
1013
The admin interface has the ability to edit models on the same page as a
997
1014
parent model. These are called inlines. Suppose you have these two models::
998
1015
 
1027
1044
The ``InlineModelAdmin`` class is a subclass of ``ModelAdmin`` so it inherits
1028
1045
all the same functionality as well as some of its own:
1029
1046
 
1030
 
``model``
1031
 
~~~~~~~~~
1032
 
 
1033
 
The model in which the inline is using. This is required.
1034
 
 
1035
 
``fk_name``
1036
 
~~~~~~~~~~~
1037
 
 
1038
 
The name of the foreign key on the model. In most cases this will be dealt
1039
 
with automatically, but ``fk_name`` must be specified explicitly if there are
1040
 
more than one foreign key to the same parent model.
1041
 
 
1042
 
``formset``
1043
 
~~~~~~~~~~~
1044
 
 
1045
 
This defaults to ``BaseInlineFormSet``. Using your own formset can give you
1046
 
many possibilities of customization. Inlines are built around
1047
 
:ref:`model formsets <model-formsets>`.
1048
 
 
1049
 
``form``
1050
 
~~~~~~~~
1051
 
 
1052
 
The value for ``form`` defaults to ``ModelForm``. This is what is
1053
 
passed through to ``inlineformset_factory`` when creating the formset for this
1054
 
inline.
 
1047
.. attribute:: InlineModelAdmin.model
 
1048
 
 
1049
    The model in which the inline is using. This is required.
 
1050
 
 
1051
.. attribute:: InlineModelAdmin.fk_name
 
1052
 
 
1053
    The name of the foreign key on the model. In most cases this will be dealt
 
1054
    with automatically, but ``fk_name`` must be specified explicitly if there
 
1055
    are more than one foreign key to the same parent model.
 
1056
 
 
1057
.. attribute:: InlineModelAdmin.formset
 
1058
 
 
1059
    This defaults to ``BaseInlineFormSet``. Using your own formset can give you
 
1060
    many possibilities of customization. Inlines are built around
 
1061
    :ref:`model formsets <model-formsets>`.
 
1062
 
 
1063
.. attribute:: InlineModelAdmin.form
 
1064
 
 
1065
    The value for ``form`` defaults to ``ModelForm``. This is what is passed
 
1066
    through to ``inlineformset_factory`` when creating the formset for this
 
1067
    inline.
1055
1068
 
1056
1069
.. _ref-contrib-admin-inline-extra:
1057
1070
 
1058
 
``extra``
1059
 
~~~~~~~~~
1060
 
 
1061
 
This controls the number of extra forms the formset will display in addition
1062
 
to the initial forms. See the
1063
 
:ref:`formsets documentation <topics-forms-formsets>` for more information.
1064
 
 
1065
 
.. versionadded:: 1.2
1066
 
 
1067
 
For users with JavaScript-enabled browsers, an "Add another" link is
1068
 
provided to enable any number of additional inlines to be added in
1069
 
addition to those provided as a result of the ``extra`` argument.
1070
 
 
1071
 
The dynamic link will not appear if the number of currently displayed
1072
 
forms exceeds ``max_num``, or if the user does not have JavaScript
1073
 
enabled.
 
1071
.. attribute:: InlineModelAdmin.extra
 
1072
 
 
1073
 
 
1074
    This controls the number of extra forms the formset will display in addition
 
1075
    to the initial forms. See the
 
1076
    :doc:`formsets documentation </topics/forms/formsets>` for more information.
 
1077
 
 
1078
    .. versionadded:: 1.2
 
1079
 
 
1080
    For users with JavaScript-enabled browsers, an "Add another" link is
 
1081
    provided to enable any number of additional inlines to be added in addition
 
1082
    to those provided as a result of the ``extra`` argument.
 
1083
 
 
1084
    The dynamic link will not appear if the number of currently displayed forms
 
1085
    exceeds ``max_num``, or if the user does not have JavaScript enabled.
1074
1086
 
1075
1087
.. _ref-contrib-admin-inline-max-num:
1076
1088
 
1077
 
``max_num``
1078
 
~~~~~~~~~~~
1079
 
 
1080
 
This controls the maximum number of forms to show in the inline. This doesn't
1081
 
directly correlate to the number of objects, but can if the value is small
1082
 
enough. See :ref:`model-formsets-max-num` for more information.
1083
 
 
1084
 
``raw_id_fields``
1085
 
~~~~~~~~~~~~~~~~~
1086
 
 
1087
 
By default, Django's admin uses a select-box interface (<select>) for
1088
 
fields that are ``ForeignKey``. Sometimes you don't want to incur the
1089
 
overhead of having to select all the related instances to display in the
1090
 
drop-down.
1091
 
 
1092
 
``raw_id_fields`` is a list of fields you would like to change
1093
 
into a ``Input`` widget for either a ``ForeignKey`` or ``ManyToManyField``::
1094
 
 
1095
 
    class BookInline(admin.TabularInline):
1096
 
        model = Book
1097
 
        raw_id_fields = ("pages",)
1098
 
 
1099
 
``template``
1100
 
~~~~~~~~~~~~
1101
 
 
1102
 
The template used to render the inline on the page.
1103
 
 
1104
 
``verbose_name``
1105
 
~~~~~~~~~~~~~~~~
1106
 
 
1107
 
An override to the ``verbose_name`` found in the model's inner ``Meta`` class.
1108
 
 
1109
 
``verbose_name_plural``
1110
 
~~~~~~~~~~~~~~~~~~~~~~~
1111
 
 
1112
 
An override to the ``verbose_name_plural`` found in the model's inner ``Meta``
1113
 
class.
 
1089
.. attribute:: InlineModelAdmin.max_num
 
1090
 
 
1091
    This controls the maximum number of forms to show in the inline. This
 
1092
    doesn't directly correlate to the number of objects, but can if the value
 
1093
    is small enough. See :ref:`model-formsets-max-num` for more information.
 
1094
 
 
1095
.. attribute:: InlineModelAdmin.raw_id_fields
 
1096
 
 
1097
    By default, Django's admin uses a select-box interface (<select>) for
 
1098
    fields that are ``ForeignKey``. Sometimes you don't want to incur the
 
1099
    overhead of having to select all the related instances to display in the
 
1100
    drop-down.
 
1101
 
 
1102
    ``raw_id_fields`` is a list of fields you would like to change into a
 
1103
    ``Input`` widget for either a ``ForeignKey`` or ``ManyToManyField``::
 
1104
 
 
1105
        class BookInline(admin.TabularInline):
 
1106
            model = Book
 
1107
            raw_id_fields = ("pages",)
 
1108
 
 
1109
 
 
1110
.. attribute:: InlineModelAdmin.template
 
1111
 
 
1112
    The template used to render the inline on the page.
 
1113
 
 
1114
.. attribute:: InlineModelAdmin.verbose_name
 
1115
 
 
1116
    An override to the ``verbose_name`` found in the model's inner ``Meta``
 
1117
    class.
 
1118
 
 
1119
.. attribute:: InlineModelAdmin.verbose_name_plural
 
1120
 
 
1121
    An override to the ``verbose_name_plural`` found in the model's inner
 
1122
    ``Meta`` class.
 
1123
 
 
1124
.. attribute:: InlineModelAdmin.can_delete
 
1125
 
 
1126
    Specifies whether or not inline objects can be deleted in the inline.
 
1127
    Defaults to ``True``.
 
1128
 
1114
1129
 
1115
1130
Working with a model with two or more foreign keys to the same parent model
1116
1131
---------------------------------------------------------------------------
1189
1204
 
1190
1205
In all other respects, the ``InlineModelAdmin`` is exactly the same as any
1191
1206
other. You can customize the appearance using any of the normal
1192
 
``InlineModelAdmin`` properties.
 
1207
``ModelAdmin`` properties.
1193
1208
 
1194
1209
Working with Many-to-Many Intermediary Models
1195
1210
----------------------------------------------
1281
1296
 
1282
1297
``django.contrib.contenttypes.generic`` provides both a ``GenericTabularInline``
1283
1298
and ``GenericStackedInline`` and behave just like any other inline. See the
1284
 
:ref:`contenttypes documentation <ref-contrib-contenttypes>` for more specific
 
1299
:doc:`contenttypes documentation </ref/contrib/contenttypes>` for more specific
1285
1300
information.
1286
1301
 
1287
1302
Overriding Admin Templates