嗯,我想出了一個解決方案,雖然很粗糙。如果其他人在添加&時將類似的問題添加到由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註冊以處理新的用戶模型。我做了更改看到here。這應該不會影響django註冊的功能,如果你碰巧在另一個項目中使用它。
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。我不完全確定爲什麼這是必要的,但沒有它我會得到錯誤。我相信這就是說,「嘿模特,爲更多的領域做好準備」。 這基本上是複製/註冊/後端/默認/ 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進行其他任何操作,請不要這樣做。你可以嘗試在你的應用中重寫模型。不管怎麼說,在註冊/ 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)更新你的URL文件:
#appName/urls.py
urlpatterns = patterns('',
url(r'^accounts/register/$', CustomRegistrationView.as_view(
form_class=RegistrationForm)),
)
希望有人在什麼地方得到一些使用了這一點。快樂的Django-ing!