好吧,我想出了一个解决方案,尽管是一个粗略的解决方案。如果其他人在向 django-registration 1.0 提供的注册页面添加和保存额外用户字段方面遇到类似问题,希望这会有所帮助......(虽然我不能保证这实际上对你有用)
1)在应用程序中创建一个新的用户模型,添加你喜欢的额外字段:
# posts/models.py
from django.contrib.auth.models import AbstractUser
class eduuser(AbstractUser):
# Created USER_TYPE_CHOICES, omitted here due to length
user_type = models.CharField(max_length=3, choices=USER_TYPE_CHOICES)
2) 将用户模型添加到您的设置文件中。我的应用恰好被称为“帖子”,所以我的用户模型是:
# appName/settings.py
AUTH_USER_MODEL = "posts.eduuser"
3) 调整 django-registration 以应对新的用户模型。我做了这里看到的改变。如果您碰巧在另一个项目中使用它,这应该不会影响 django-registration 的功能。
4) 在表单中添加额外的字段:
# posts/forms.py
from registration.forms import RegistrationForm
from django import forms
from django.forms import ModelForm
from .models import eduuser
class eduuserForm(forms.ModelForm):
"""
Get extra 'user_type' field to add to form for django-registration
"""
class Meta:
model = eduuser
fields = ('user_type',)
RegistrationForm.base_fields.update(eduuserForm.base_fields)
5) 覆盖 RegistrationView。我不完全确定为什么这是必要的,但是没有它我会出错。我相信它是在说,“嘿模型,为额外的领域做好准备”。这基本上是从registration/backends/default/views.py复制/粘贴,但添加了额外的字段
# posts/views.py
from registration.backends.default.views import RegistrationView
from registration import signals
from registration.models import RegistrationProfile
from django.contrib.sites.models import Site
from django.contrib.sites.models import RequestSite
class CustomRegistrationView(RegistrationView):
"""
Needed override this django-registration feature to have it create
a profile with extra field
"""
def register(self, request, **cleaned_data):
username, email, password, user_type = cleaned_data['username'], cleaned_data['email'], cleaned_data['password1'], cleaned_data['user_type']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(
username, email, password, user_type, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
6) 更新模型。我通过直接编辑 django-registration 模型来做到这一点(因为它是一个孤狼项目,这是我唯一的用途),但如果您将 django-registration 用于其他任何事情,请不要这样做。我认为您可以尝试在您的应用程序中覆盖模型。无论如何,在 registration/models.py 我添加了额外的字段:
# registration/models.py
def create_inactive_user(self, username, email, password, user_type,
site, send_email=True):
"""
Create a new, inactive ``User``, generate a
``RegistrationProfile`` and email its activation key to the
``User``, returning the new ``User``.
By default, an activation email will be sent to the new
user. To disable this, pass ``send_email=False``.
"""
new_user = get_user_model().objects.create_user(username, email, password, user_type=user_type)
new_user.is_active = False
new_user.save()
registration_profile = self.create_profile(new_user)
if send_email:
registration_profile.send_activation_email(site)
return new_user
7)更新你的网址文件:
#appName/urls.py
urlpatterns = patterns('',
url(r'^accounts/register/$', CustomRegistrationView.as_view(
form_class=RegistrationForm)),
)
希望有人,在某个地方能从中得到一些用处。姜戈快乐!