我通过执行创建User Profile应用程序和扩展User模块的整个过程为User添加了一个额外的字段.
它似乎没有错误.
我无法弄清楚或在任何地方找到如何在管理员创建新用户的页面中显示此新字段.因此,根据个人信息,如名字和姓氏,我希望有我添加到用户个人资料的位置字段.
我的用户资料:
from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save class UserProfile(models.Model): # This field is required. user = models.OneToOneField(User) # Other fields here location = models.CharField(max_length=20) # definition of UserProfile from above # ... def create_user_profile(sender,instance,created,**kwargs): if created: UserProfile.objects.create(user=instance) post_save.connect(create_user_profile,sender=User)
我也想知道如何强制要求电子邮件,如密码和用户名.只需将Django文件夹中的用户模型更改为:
email = models.EmailField(_('e-mail address'),unique=True)
完全不起作用.
[UPDATE]
所以这是我创建的admin.py帽子.
我应该在settings.py文件中将其包含在哪里,以便实际使用添加了用户模块和新表单的文件夹?
我有这条线,但似乎根本没有使用新的形式
AUTH_PROFILE_MODULE =’UserProfile.UserProfile’
(我有一个名为UserProfile的文件夹,其中包含两个snipets代码)
from django.contrib import admin from django.contrib.auth.models import User,Group from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm,UserChangeForm from django import forms from django.contrib.admin.views.main import * class MyUserCreationForm(UserCreationForm): """ A form that creates a user,with no privileges,from the given username and password. """ OFFICES = ( (0,"Global"),(1,"Dublin"),(2,"Tokyo"),(3,"Warsaw"),(4,"Beijing"),(5,"SEOul"),(6,"Taipei"),(7,"Orem"),(8,"Mountain View"),(9,"San Luis Obispo"),(10,"Roseville"),(11,"Pune"),(12,"i18n") ) username = forms.RegexField(label=_("Username"),max_length=30,regex=r'^[\w.@+-]+$',help_text = _("required. 30 characters or fewer. Letters,digits and @/./+/-/_ only."),error_messages = {'invalid': _("This value may contain only letters,numbers and @/./+/-/_ characters.")}) password1 = forms.CharField(label=_("Password"),widget=forms.PasswordInput) password2 = forms.CharField(label=_("Password confirmation"),widget=forms.PasswordInput,help_text = _("Enter the same password as above,for verification.")) location = forms.IntegerField(label=_("Location"),choices=TYPE_CHOICES) class Meta: model = User fields = ("username",) def clean_username(self): username = self.cleaned_data["username"] try: User.objects.get(username=username) except User.DoesNotExist: return username raise forms.ValidationError(_("A user with that username already exists.")) def clean_password2(self): password1 = self.cleaned_data.get("password1","") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.ValidationError(_("The two password fields didn't match.")) return password2 def save(self,commit=True): user = super(UserCreationForm,self).save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class CustomUserAdmin(UserAdmin): add_form = MyUserCreationForm inlines = [ProfileInline,] add_fieldsets = ( (None,{ 'classes': ('wide',),'fields': ('username','email','password1','password2','location')} ),) admin.site.unregister(User) admin.site.register(User,CustomUserAdmin) admin.site.register(Class,ClassAdmin)
解决方法
您需要使用自己的UserAdmin类并修改add_fieldsets属性以更改显示的字段.
See this Stack Overflow question for an example.
如果要与用户同时编辑UserProfile实例,一种方法是将UserProfile作为内联添加到自定义UserAdmin.希望能帮到你.
#admin.py from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User admin.site.unregister(User) class MyUserAdmin(UserAdmin): add_fieldsets = ( (None,'password2')} ),) admin.site.register(User,MyUserAdmin)