Code

Ticket #7932: user_model_admin_subclass.diff

File user_model_admin_subclass.diff, 2.1 KB (added by ElliottM, 6 years ago)
Line 
1Index: django/contrib/auth/admin.py
2===================================================================
3--- django/contrib/auth/admin.py        (revision 8066)
4+++ django/contrib/auth/admin.py        (working copy)
5@@ -1,4 +1,5 @@
6 from django.contrib.auth.models import User, Group
7+from django.contrib.auth.forms import UserCreationForm
8 from django.core.exceptions import PermissionDenied
9 from django import template
10 from django.shortcuts import render_to_response
11@@ -24,14 +25,14 @@
12     search_fields = ('username', 'first_name', 'last_name', 'email')
13     ordering = ('username',)
14     filter_horizontal = ('user_permissions',)
15+
16+    creation_form=UserCreationForm
17
18     def add_view(self, request):
19-        # avoid a circular import. see #6718.
20-        from django.contrib.auth.forms import UserCreationForm
21         if not self.has_change_permission(request):
22             raise PermissionDenied
23         if request.method == 'POST':
24-            form = UserCreationForm(request.POST)
25+            form = self.creation_form(request.POST)
26             if form.is_valid():
27                 new_user = form.save()
28                 msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
29@@ -42,7 +43,7 @@
30                     request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
31                     return HttpResponseRedirect('../%s/' % new_user.id)
32         else:
33-            form = UserCreationForm()
34+            form = self.creation_form()
35         return render_to_response('admin/auth/user/add_form.html', {
36             'title': _('Add user'),
37             'form': form,
38@@ -55,9 +56,9 @@
39             'has_file_field': False,
40             'has_absolute_url': False,
41             'auto_populated_fields': (),
42-            'opts': User._meta,
43+            'opts': self.model._meta,
44             'save_as': False,
45-            'username_help_text': User._meta.get_field('username').help_text,
46+            'username_help_text': self.model._meta.get_field('username').help_text,
47             'root_path': self.admin_site.root_path,
48         }, context_instance=template.RequestContext(request))
49