我已经完成了解决方案here和Shacker的django-profiles: The Missing Manual及后续信号解决方案Dmitko并回答了here以及许多地方。
以下是详细信息:
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的这个解决方案应该放在一个地方,并将最佳实践放在一起以便于使用。
帮助。
答案 0 :(得分:0)
您的Member
模型定义在哪里?我甚至无法看到它被ProfileForm
导入。您是将ProfileForm
和UserRegistrationForm
放在同一页面上吗?
如果用户填写了两个表单(甚至填写了ProfileForm
),那么您的save()
ProfileForm
中只需创建一个信号就可以了您的个人资料:
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()