Tôi đã thêm trường bổ sung cho Người dùng bằng cách thực hiện toàn bộ quá trình tạo ứng dụng Hồ sơ người dùng và mở rộng mô-đun User.Mở rộng biểu mẫu người dùng mới, trong quản trị Django
Dường như không có lỗi. Những gì tôi không thể tìm ra, hoặc tìm bất cứ nơi nào là cách hiển thị trường mới này trong trang nơi quản trị viên tạo người dùng mới. Vì vậy, theo thông tin cá nhân như First name và Last Name, tôi muốn có trường Location mà tôi đã thêm vào User Profile.
Hồ sơ người dùng của tôi:
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)
cũng Tôi muốn biết làm thế nào để làm cho email bắt buộc, như mật khẩu và tên người dùng. Chỉ cần thay đổi mô hình người dùng trong thư mục Django thành:
email = models.EmailField(_('e-mail address'), unique=True)
không hoạt động.
[UPDATE] Vì vậy, đây là chiếc mũ admin.py mà tôi đã tạo. Tôi nên bao gồm điều này trong tệp settings.py để nó thực sự sử dụng thư mục với mô-đun người dùng đã thêm và biểu mẫu mới? Tôi có dòng này nhưng nó không có vẻ để sử dụng các hình thức mới ở tất cả các AUTH_PROFILE_MODULE = 'UserProfile.UserProfile' (tôi có một thư mục có tên UserProfile trong đó có hai snipets mã)
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)
Tôi xin lỗi, tôi chỉ cố gắng để thực hiện điều này bây giờ, kể từ khi tôi didnt có thời gian trước và tôi nhận được bản thân mình bối rối vì tất cả các liên kết. Tôi đoán tôi cần phải tạo một tập tin mới .py với mã cho lớp MyUserAdmin (UserAdmin): bạn chỉ định trong http://stackoverflow.com/questions/6628452/how-can-i-have-django-user-registration -single-step-thay-cho-hai-stepprocess/6630174 # 6630174? – Angie
Có, bạn có thể đặt mã vào tệp admin.py của riêng bạn – Brandon
Tôi đã cập nhật mã của mình, nhưng không chắc chắn cách làm cho trang web của tôi thực sự sử dụng nó. Cho đến nay chỉ là một thư mục chứa các tệp .py không liên kết với dự án chính – Angie