我已經Dmitko通過溶液here和Shacker的django-profiles: The Missing Manual和後續信號的解決方案去和答案here很多地方空白Django的配置文件。Django的註冊創建一個使用信號
下面是詳細信息:
AUTH_PROFILE_MODULE = "members.Profile
「
從members.models.py
class Profile(models.Model):
"""member model."""
GENDER_CHOICES = (
(1, 'Male'),
(2, 'Female'),
)
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(_('first name'), max_length=100)
middle_name = models.CharField(_('middle name'), blank=True, max_length=100)
last_name = models.CharField(_('last name'), max_length=100)
slug = models.SlugField(_('slug'), unique=True)
gender = models.PositiveSmallIntegerField(
_('gender'),
choices=GENDER_CHOICES,
blank=True,
null=True,
)
birth_date = models.DateField(_('birth date'), blank=True, null=True)
class Meta:
verbose_name = _('Profiles')
verbose_name_plural = _('Profiles')
db_table = 'Profiles'
ordering = ('last_name', 'first_name',)
從members.forms.py:
from django.contrib.auth.models import User
from basic.members.models import Profile
from registration.forms import RegistrationForm, RegistrationFormTermsOfService, RegistrationFormUniqueEmail
from django.db.models import signals
from basic.members.signals import create_profile
attrs_dict = { 'class': 'required' }
class ProfileForm(RegistrationFormTermsOfService, RegistrationFormUniqueEmail):
GENDER_CHOICES = (
(1, 'Male'),
(2, 'Female'),
)
first_name = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
last_name = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
gender = forms.IntegerField(widget=forms.RadioSelect(choices=GENDER_CHOICES))
birth_date = forms.DateField(widget=SelectDateWidget(years=range(1911,1999)))
def save(self, user):
try:
data = user.get_profile()
except:
data = Profile(user=user)
data.first_name = self.cleaned_data["first_name"]
data.first_name = self.cleaned_data["last_name"]
data.save()
class UserRegistrationForm(RegistrationForm):
first_name = forms.CharField(max_length=100)
last_name = forms.CharField(max_length=100)
signals.post_save.connect(create_profile, sender=User)
從members.signals .py:
def create_profile(sender, instance, signal, created, **kwargs):
"""When user is created also create a matching profile."""
from basic.members.models import Profile
if created:
Profile(user = instance).save()
而且members.regbackend.py:
def create_profile(sender, instance, signal, created, **kwargs):
"""When user is created also create a matching profile."""
from basic.members.models import Profile
if created:
Profile(user = instance).save()
我卡住了。基本上我的ProfileForm將字段添加到用戶註冊頁面,但在保存時,它將創建一個用戶對象,其中用戶名爲電子郵件地址,但用戶對象和配置文件對象中的名字和姓氏不會保存。但是,空白配置文件是使用空白字段創建的。我一定錯過了一些小東西,但我似乎無法看到它。
所以信號工作,但該數據沒有被保存到配置文件。
老實說,這個解決方案在我們心愛的Django都應該在一個地方與最佳實踐放在一起,便於。
幫助。
嗨盧克,我的編輯錯誤。編輯條目。我刪除了UserRegistrationForm和signals.post_save.connect(..),現在我沒有得到空白配置文件。 – Afrowave 2011-01-06 15:17:54