Django版本: 4.0.2 Django-注册版本: 0.8应用名称: userAccounts
问题:我正在尝试创建一个叫做教授的用户类型来扩展用户模型。我可以完成注册过程,问题是只有用户保存在数据库中,教授表保留为空。所以我认为问题可能出在save方法上。有没有什么问题的线索?
SETTINGS.PY
AUTH_PROFILE_MODULE = "userAccounts.Professor"MODELS.PY -在这里我创建了从user和一个示例字段扩展的模型
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
发布于 2013-02-19 05:53:40
已解决的问题-解决方案
已经有几天了,但我找到了!:D我在shacker上找到了这篇文章:Django-Registration & Django-Profile, using your own custom form
显然,在django注册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在此行配置文件url之前包含根配置文件(r‘/’,(‘profiles.urls’)),
url(r'^accounts/registrar_usuario/$',
'registration.views.register',
{'backend': 'Hesperides.apps.accounts.user_regbackend.DefaultBackend','form_class':user_RegistrationForm},
name='registration_register'
),感谢
感谢您的耐心,并向我展示了非常非常有用的pdb。来自西班牙的问候。还有: shacker和dmitko为我指路
https://stackoverflow.com/questions/14764248
复制相似问题