我有一个名为“组织”的模型,我已将其设置为用户个人资料,我希望“组织”模型中的字段显示在注册页面上。我如何使用django-registration进行此操作。
# models.py
class Organization(models.Model):
user = models.ForeignKey(User, unique=True)
logo = models.ImageField(upload_to='organizations')
name = models.CharField(max_length=100, null=True, unique=True)
# more fields below etc.
# settings.py
AUTH_PROFILE_MODULE = 'volunteering.organization'
答案 0 :(得分:6)
最简单的方法是[在django-registration
0.8]上测试:
项目中的某个地方,例如组织应用中的forms.py
from registration.forms import RegistrationForm
from django.forms import ModelForm
from models import Organization
class OrganizationForm(forms.ModelForm):
class Meta:
model = Organization
RegistrationForm.base_fields.update(OrganizationForm.base_fields)
class CustomRegistrationForm(RegistrationForm):
def save(self, profile_callback=None):
user = super(CustomRegistrationForm, self).save(profile_callback=None)
org, c = Organization.objects.get_or_create(user=user, \
logo=self.cleaned_data['logo'], \
name=self.cleaned_data['name'])
然后在您的root urlconf中[但在包含registration.urls
的正则表达式模式之上并假设正则表达式为r'^accounts/'
]添加:
from organization.forms import CustomRegistrationForm
urlpatterns += patterns('',
(r'^accounts/register/$', 'registration.views.register', {'form_class':CustomRegistrationForm}),
)
显然,您也可以create a custom backend,但恕我直言,这样会更容易。
答案 1 :(得分:2)
最好的方法是在应用程序中创建组织文件(例如“forms.py”),并执行以下操作:
from registration.forms import RegistrationForm
from forms import *
from models import Organization
class RegistrationFormWithOrganization(RegistrationForm):
organization_logo = field.ImageField()
organization_name = field.CharField()
def save(self, profile_callback = None):
Organization.objects.get_or_create(user = self.cleaned_data['user'],
logo = self.cleaned_data['organization_logo'],
name = self.cleaned_data['organization_name'])
super(RegistrationFormWithOrganization, self).save(self, profile_callback)
然后在您的基本网址中,覆盖现有的注册网址,并将此表单添加为要使用的表单:
form organization.forms import RegistrationFormWithOrganization
url('^/registration/register$', 'registration.views.register',
{'form_class': RegistrationFormWithOrganization}),
url('^/registration/', include('registration.urls')),
请记住,Django将使用与正则表达式匹配的第一个URL,因此将匹配您的调用而不是django-registration。它还会告诉注册使用您的表单,而不是它自己的表单。我在这里省略了很多验证(也许,可能是用户对象的派生......如果是这样,请阅读源代码进行注册以查看它来自何处),但这绝对是获得的正确途径只需花费很少的精力就可以进入页面。
答案 2 :(得分:1)
修改如下代码,然后重试
urlpatterns += patterns('',
(r'^accounts/register/$', 'registration.views.register', {'form_class':CustomRegistrationForm,'backend': 'registration.backends.default.DefaultBackend'}),
)
答案 3 :(得分:0)
“以前,用于在注册期间收集数据的表单应该实现一个save()方法,该方法将创建新的用户帐户。不再是这种情况;创建帐户由后端处理,所以任何自定义逻辑应该移动到自定义中 后端,或通过将监听器连接到注册过程中发送的信号。“
详细信息:
可以找到更多信息here