summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2012-10-18 20:12:41 -0400
committerTim Graham <timograham@gmail.com>2012-10-20 08:42:24 -0400
commite7685b87c13e87d429dd30065373d8dd2f20a2e4 (patch)
treeb3e5f95f98140beb9499dcab151003b5070ba357
parent700717db1f7e3032cfd89ea80f37eb69bc54a188 (diff)
[1.4.X] Fixed #17006 - Documented ModelAdmin get_form() and get_formsets()
Backport of eed4faf16f from master
-rw-r--r--docs/ref/contrib/admin/index.txt41
1 files changed, 37 insertions, 4 deletions
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
index d3154d6dd8..43446353dd 100644
--- a/docs/ref/contrib/admin/index.txt
+++ b/docs/ref/contrib/admin/index.txt
@@ -314,7 +314,9 @@ subclass::
By default a ``ModelForm`` is dynamically created for your model. It is
used to create the form presented on both the add/change pages. You can
easily provide your own ``ModelForm`` to override any default form behavior
- on the add/change pages.
+ on the add/change pages. Alternatively, you can customize the default
+ form rather than specifying an entirely new one by using the
+ :meth:`ModelAdmin.get_form` method.
For an example see the section `Adding custom validation to the admin`_.
@@ -380,7 +382,8 @@ subclass::
.. attribute:: ModelAdmin.inlines
- See :class:`InlineModelAdmin` objects below.
+ See :class:`InlineModelAdmin` objects below as well as
+ :meth:`ModelAdmin.get_formsets`.
.. attribute:: ModelAdmin.list_display
@@ -1130,6 +1133,38 @@ templates used by the :class:`ModelAdmin` views:
(r'^my_view/$', self.admin_site.admin_view(self.my_view, cacheable=True))
+.. method:: ModelAdmin.get_form(self, request, obj=None, **kwargs)
+
+ Returns a :class:`~django.forms.ModelForm` class for use in the admin add
+ and change views, see :meth:`add_view` and :meth:`change_view`.
+
+ If you wanted to hide a field from non-superusers, for example, you could
+ override ``get_form`` as follows::
+
+ class MyModelAdmin(admin.ModelAdmin):
+ def get_form(self, request, obj=None, **kwargs):
+ self.exclude = []
+ if not request.user.is_superuser:
+ self.exclude.append('field_to_hide')
+ return super(MyModelAdmin, self).get_form(request, obj, **kwargs)
+
+.. method:: ModelAdmin.get_formsets(self, request, obj=None)
+
+ Yields :class:`InlineModelAdmin`\s for use in admin add and change views.
+
+ For example if you wanted to display a particular inline only in the change
+ view, you could override ``get_formsets`` as follows::
+
+ class MyModelAdmin(admin.ModelAdmin):
+ inlines = [MyInline, SomeOtherInline]
+
+ def get_formsets(self, request, obj=None):
+ for inline in self.get_inline_instances():
+ # hide MyInline in the add view
+ if isinstance(inline, MyInline) and obj is None:
+ continue
+ yield inline.get_formset(request, obj)
+
.. method:: ModelAdmin.formfield_for_foreignkey(self, db_field, request, **kwargs)
The ``formfield_for_foreignkey`` method on a ``ModelAdmin`` allows you to
@@ -1454,8 +1489,6 @@ The ``InlineModelAdmin`` class adds:
through to ``inlineformset_factory`` when creating the formset for this
inline.
- .. _ref-contrib-admin-inline-extra:
-
.. attribute:: InlineModelAdmin.extra
This controls the number of extra forms the formset will display in