Django版本:4.0.2 Django-Registration版本:0.8 应用名称:userAccounts
问题: 我试图创建一个名为教授的用户类型来扩展用户模型。我可以完成注册过程,问题是只有用户保存在DB上,教授表保持空白。所以我认为问题可能出在save方法中。关于可能出现什么问题的一些线索?
SETTINGS.PY
AUTH_PROFILE_MODULE = "userAccounts.Professor"
MODELS.PY - 在这里,我创建了从用户和示例字段扩展的模型
from django.db import models
from django.contrib.auth.models import User
class Professor(models.Model):
user = models.ForeignKey(User, unique=True)
exampleField = models.CharField(max_length=100)
FORMS.PY - 保存用户后,我会在教授表中保存new_profile。
from django import forms
from registration.forms import RegistrationForm
from models import Professor
from registration.models import RegistrationProfile
class CustomRegistrationForm(RegistrationForm):
exampleField = forms.CharField()
def save(self, profile_callback=None):
new_user = RegistrationProfile.objects.create_inactive_user(
username=self.cleaned_data['username'],
password=self.cleaned_data['password1'],
email = self.cleaned_data['email'])
new_professor = Professor(
user=new_user,
exampleField=self.cleaned_data['exampleField'])
new_professor.save()
return new_user
注意:我正在关注这篇文章:Registration form with profile's field
答案 0 :(得分:4)
问题已解决 - 解决方案
已经有好几天但我明白了! :d 我从shacker发现这篇文章: Django-Registration & Django-Profile, using your own custom form
显然在django-registration 0.8中save()方法无法覆盖。 所以,另外两个选项是: 使用信号或重写后端。这两个解决方案都在我链接的帖子中解释。如果有人有同样的问题,我会尽力帮助评论:)
注意:使用信号工作正常但我在从表单中获取值时遇到了一些问题。所以我实现了后端方法,一切顺利。我强烈建议你阅读shacker的帖子,但是,如果你真的很沮丧,我的代码是:
my forms.py
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_("Username"),
error_messages={'invalid': _("This value must contain only letters, numbers and underscores.")})
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_("Email address"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password (again)"))
#ADITIONAL FIELDS
#they are passed to the backend as kwargs and there they are saved
nombre = forms.CharField(widget=forms.TextInput(attrs=attrs_dict),
label=_("Nombre"))
__ init__.py(app / user_backend / __ init __。py)
class DefaultBackend(object):
def register(self, request, **kwargs):
username, email, password, nombre = kwargs['username'], kwargs['email'], kwargs['password1'], kwargs['nombre']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
usuario(user = User.objects.get(username=new_user.username), nombre=nombre).save()
return new_user
此行前root URLS.PY - > url(r'profiles /',include('profiles.urls')),
url(r'^accounts/registrar_usuario/$',
'registration.views.register',
{'backend': 'Hesperides.apps.accounts.user_regbackend.DefaultBackend','form_class':user_RegistrationForm},
name='registration_register'
),
<强>感谢强>
mattsnider耐心等待,并告诉我非常非常有用的pdb。来自西班牙。 并且:shacker和dmitko为我指明了方式