diff options
| author | Julien Phalip <jphalip@gmail.com> | 2014-11-16 12:25:05 +0100 |
|---|---|---|
| committer | Julien Phalip <jphalip@gmail.com> | 2014-11-16 12:25:05 +0100 |
| commit | 05e0e4674ce9995a1dc5962001747abce30e4f69 (patch) | |
| tree | 011c113a0b23314d2f9e554b253d6e4962256569 /docs | |
| parent | 4024dd0c98d71647b2001f186a92242314c987b2 (diff) | |
| parent | 0d1a9d203a970a82a2f81edf0ba7d4b55442fd78 (diff) | |
Merge pull request #3549 from psagers/master
Fixes a race condition in the documentation.
The example for django.contrib.admin.ModelAdmin.get_form was modifying self.exclude. However, since ModelAdmin instances are global and have no thread- or request-locality, this is not safe for concurrent requests. This updated documentation demonstrates a safe method to override admin forms on a per-request basis.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/contrib/admin/index.txt | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index f684452e23..4c06a7575e 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -1488,16 +1488,20 @@ templates used by the :class:`ModelAdmin` views: 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:: + The base implementation uses :func:`~django.forms.models.modelform_factory` + to subclass :attr:`~form`, modified by attributes such as :attr:`~fields` + and :attr:`~exclude`. So, for example, if you wanted to offer additional + fields to superusers, you could swap in a different base form like so:: 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') + if request.user.is_superuser: + kwargs['form'] = MySuperuserForm return super(MyModelAdmin, self).get_form(request, obj, **kwargs) + You may also simply return a custom :class:`~django.forms.ModelForm` class + directly. + .. method:: ModelAdmin.get_formsets(request, obj=None) .. deprecated:: 1.7 |
