mirror of
				https://github.com/django/django.git
				synced 2025-10-29 00:26:07 +00:00 
			
		
		
		
	[1.7.x] Fixes a race condition in the documentation.
The example for django.contrib.admin.ModelAdmin.get_form modifies
self.exclude. However, since ModelAdmin instances are global and have no
thread- or request-locality, this is not safe for concurrent
requests.[1] This updated documentation demonstrates a safe method to
override admin forms on a per-request basis.
[1] https://groups.google.com/forum/#!topic/django-users/AmoUDtEefyA
Backport of 0d1a9d203a from master
			
			
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							7796f62c36
						
					
				
				
					commit
					42001e027c
				
			| @@ -1475,16 +1475,20 @@ templates used by the :class:`ModelAdmin` views: | |||||||
|     Returns a :class:`~django.forms.ModelForm` class for use in the admin add |     Returns a :class:`~django.forms.ModelForm` class for use in the admin add | ||||||
|     and change views, see :meth:`add_view` and :meth:`change_view`. |     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 |     The base implementation uses :func:`~django.forms.models.modelform_factory` | ||||||
|     override ``get_form`` as follows:: |     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): |         class MyModelAdmin(admin.ModelAdmin): | ||||||
|             def get_form(self, request, obj=None, **kwargs): |             def get_form(self, request, obj=None, **kwargs): | ||||||
|                 self.exclude = [] |                 if request.user.is_superuser: | ||||||
|                 if not request.user.is_superuser: |                     kwargs['form'] = MySuperuserForm | ||||||
|                     self.exclude.append('field_to_hide') |  | ||||||
|                 return super(MyModelAdmin, self).get_form(request, obj, **kwargs) |                 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) | .. method:: ModelAdmin.get_formsets(request, obj=None) | ||||||
|  |  | ||||||
|     .. deprecated:: 1.7 |     .. deprecated:: 1.7 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user