mirror of
https://github.com/django/django.git
synced 2025-09-26 12:09:19 +00:00
Fixed #18388 - Added InlineModelAdmin.get_max_num hook.
Thanks d.willy.c.c@ for the suggestion and Melevir and Areski Belaid for work on the patch.
This commit is contained in:
parent
07a73a2714
commit
61524b09cf
5 changed files with 47 additions and 5 deletions
|
@ -1728,6 +1728,11 @@ The ``InlineModelAdmin`` class adds:
|
|||
doesn't directly correlate to the number of objects, but can if the value
|
||||
is small enough. See :ref:`model-formsets-max-num` for more information.
|
||||
|
||||
.. versionadded:: 1.6
|
||||
|
||||
:meth:`InlineModelAdmin.get_max_num` also allows you to customize the
|
||||
maximum number of extra forms.
|
||||
|
||||
.. attribute:: InlineModelAdmin.raw_id_fields
|
||||
|
||||
By default, Django's admin uses a select-box interface (<select>) for
|
||||
|
@ -1787,6 +1792,27 @@ The ``InlineModelAdmin`` class adds:
|
|||
return extra - obj.binarytree_set.count()
|
||||
return extra
|
||||
|
||||
.. method:: InlineModelAdmin.get_max_num(self, request, obj=None, **kwargs)
|
||||
|
||||
.. versionadded:: 1.6
|
||||
|
||||
Returns the maximum number of extra inline forms to use. By default,
|
||||
returns the :attr:`InlineModelAdmin.max_num` attribute.
|
||||
|
||||
Override this method to programmatically determine the maximum number of
|
||||
inline forms. For example, this may be based on the model instance
|
||||
(passed as the keyword argument ``obj``)::
|
||||
|
||||
class BinaryTreeAdmin(admin.TabularInline):
|
||||
model = BinaryTree
|
||||
|
||||
def get_max_num(self, request, obj=None, **kwargs):
|
||||
max_num = 10
|
||||
if obj.parent:
|
||||
return max_num - 5
|
||||
return max_num
|
||||
|
||||
|
||||
Working with a model with two or more foreign keys to the same parent model
|
||||
---------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -292,9 +292,10 @@ Minor features
|
|||
:meth:`~django.db.models.query.QuerySet.select_related` can be cleared using
|
||||
``select_related(None)``.
|
||||
|
||||
* The :meth:`~django.contrib.admin.InlineModelAdmin.get_extra` method on
|
||||
* The :meth:`~django.contrib.admin.InlineModelAdmin.get_extra` and
|
||||
:meth:`~django.contrib.admin.InlineModelAdmin.get_max_num` methods on
|
||||
:class:`~django.contrib.admin.InlineModelAdmin` may be overridden to
|
||||
customize the number of extra inline forms.
|
||||
customize the extra and maximum number of inline forms.
|
||||
|
||||
Backwards incompatible changes in 1.6
|
||||
=====================================
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue