我已經爲用戶添加了一個額外的字段,通過完成製作User Profile應用程序和擴展用戶模塊的整個過程。擴展新的用戶表單,在管理Django
它似乎沒有錯誤。 我無法弄清楚,或找到任何地方是如何顯示這個新的領域,在管理員創建一個新用戶的頁面。因此,在名字和姓氏等個人信息下,我希望在那裏有我添加到用戶個人資料中的位置字段。
我的用戶配置文件:
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)
根本不起作用。
[更新] 所以這是我創建的我的admin.py帽子。 我應該在settings.py文件中包含它,以便它實際上使用帶有添加的用戶模塊和新窗體的文件夾? 我有這條線,但它似乎沒有使用新的形式在所有 AUTH_PROFILE_MODULE =「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'^[\[email protected]+-]+$',
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)
我很抱歉,我只是想現在實現這一點,因爲我沒有時間前後由於所有鏈接,我都感到困惑。我猜我需要用類MyUserAdmin(UserAdmin)的代碼創建一個新文件.py:你在http://stackoverflow.com/questions/6628452/how-can-i-have-django-user-registration中指定 - 單步驟而不是兩步法/ 6630174#6630174? – Angie
是的,你可以把代碼放到你自己的admin.py文件中 – Brandon
我更新了我的代碼,但不知道如何讓我的網站實際使用它。到目前爲止,它只是一個文件夾,其中沒有鏈接到主項目 – Angie