475
471
.. attribute:: ModelAdmin.list_select_related
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
481
478
The value should be either ``True`` or ``False``. Default is ``False``.
483
Note that Django will use ``select_related()``, regardless of this setting,
484
if one of the ``list_display`` fields is a ``ForeignKey``.
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
489
484
.. attribute:: ModelAdmin.inlines
595
590
somebody submits a search query in that text box.
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::
601
596
search_fields = ['foreign_key__related_fieldname']
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::
601
search_fields = ['user__email']
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
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)
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.
873
.. method:: ModelAdmin.formfield_for_manytomany(self, db_field, request, **kwargs)
875
.. versionadded:: 1.1
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``::
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)
874
889
.. method:: ModelAdmin.queryset(self, request)
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:
1033
The model in which the inline is using. This is required.
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.
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>`.
1052
The value for ``form`` defaults to ``ModelForm``. This is what is
1053
passed through to ``inlineformset_factory`` when creating the formset for this
1047
.. attribute:: InlineModelAdmin.model
1049
The model in which the inline is using. This is required.
1051
.. attribute:: InlineModelAdmin.fk_name
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.
1057
.. attribute:: InlineModelAdmin.formset
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>`.
1063
.. attribute:: InlineModelAdmin.form
1065
The value for ``form`` defaults to ``ModelForm``. This is what is passed
1066
through to ``inlineformset_factory`` when creating the formset for this
1056
1069
.. _ref-contrib-admin-inline-extra:
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.
1065
.. versionadded:: 1.2
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.
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
1071
.. attribute:: InlineModelAdmin.extra
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.
1078
.. versionadded:: 1.2
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.
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.
1075
1087
.. _ref-contrib-admin-inline-max-num:
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.
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
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``::
1095
class BookInline(admin.TabularInline):
1097
raw_id_fields = ("pages",)
1102
The template used to render the inline on the page.
1107
An override to the ``verbose_name`` found in the model's inner ``Meta`` class.
1109
``verbose_name_plural``
1110
~~~~~~~~~~~~~~~~~~~~~~~
1112
An override to the ``verbose_name_plural`` found in the model's inner ``Meta``
1089
.. attribute:: InlineModelAdmin.max_num
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.
1095
.. attribute:: InlineModelAdmin.raw_id_fields
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
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``::
1105
class BookInline(admin.TabularInline):
1107
raw_id_fields = ("pages",)
1110
.. attribute:: InlineModelAdmin.template
1112
The template used to render the inline on the page.
1114
.. attribute:: InlineModelAdmin.verbose_name
1116
An override to the ``verbose_name`` found in the model's inner ``Meta``
1119
.. attribute:: InlineModelAdmin.verbose_name_plural
1121
An override to the ``verbose_name_plural`` found in the model's inner
1124
.. attribute:: InlineModelAdmin.can_delete
1126
Specifies whether or not inline objects can be deleted in the inline.
1127
Defaults to ``True``.
1115
1130
Working with a model with two or more foreign keys to the same parent model
1116
1131
---------------------------------------------------------------------------