Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向django注册表添加额外字段_Python_Django_Forms_Models_Django Registration - Fatal编程技术网

Python 向django注册表添加额外字段

Python 向django注册表添加额外字段,python,django,forms,models,django-registration,Python,Django,Forms,Models,Django Registration,我有一个名为“Organization”的模型,我已经将其设置为用户配置文件,我希望“Organization”模型中的字段显示在注册页面上。我该如何使用django注册来实现这一点 # models.py class Organization(models.Model): user = models.ForeignKey(User, unique=True) logo = models.ImageField(upload_to='organizations') name

我有一个名为“Organization”的模型,我已经将其设置为用户配置文件,我希望“Organization”模型中的字段显示在注册页面上。我该如何使用django注册来实现这一点

# 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'

最好的方法是在应用程序中创建一个组织文件(比如“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)
然后在基本URL中,覆盖现有URL以进行注册,并将此表单添加为要使用的表单:

 form organization.forms import RegistrationFormWithOrganization

 url('^/registration/register$', 'registration.views.register', 
     {'form_class': RegistrationFormWithOrganization}),
 url('^/registration/', include('registration.urls')),

请记住,Django将使用与regexp匹配的第一个URL,因此将匹配您的调用,而不是Django注册。它还会告诉注册人员使用您的表格,而不是自己的表格。我在这里跳过了很多验证(可能还有用户对象的派生……如果是的话,请阅读源代码进行注册,看看它是从哪里来的),但这绝对是一条正确的途径,您只需花费最少的精力就可以在页面中添加一些内容。

最简单的方法是[在django注册时测试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'])
然后在根urlconf中[但在包含
registration.urls的regex模式上方
并假设regex是
r'^accounts/'
]添加:

from organization.forms import CustomRegistrationForm

urlpatterns += patterns('',
    (r'^accounts/register/$', 'registration.views.register',    {'form_class':CustomRegistrationForm}),
)

显然,您也可以这样做,但我认为这更简单。

按如下方式修改代码,然后重试

urlpatterns += patterns('',
(r'^accounts/register/$', 'registration.views.register',    {'form_class':CustomRegistrationForm,'backend': 'registration.backends.default.DefaultBackend'}),
)

“以前,注册期间用于收集数据的表单需要实现一个save()方法,该方法将创建新的用户帐户。现在不再是这种情况;创建帐户由后端处理,因此任何自定义逻辑都应该移到自定义逻辑中 后端,或通过将侦听器连接到注册过程中发送的信号。”

详情:


可以找到更多信息

这将不起作用,因为(a)
RegistrationForm
没有
user
字段,因此
self.cleaned_data['user']
将抛出
keyrerror
异常;(b)您需要一个
User
对象来创建一个
Organization
对象,在调用
RegistrationForm之前,您无法获得该对象。save
将返回新创建的
User
对象。请查看我的答案,以获得替代解决方案。@SimonKagwi我使用了您的答案,但我一直收到这个错误:TypeError at/accounts/register/register()至少接受2个非关键字参数(1个给定)我经常遇到这样的错误:register()至少接受2个非关键字参数(1个给定)。这是由于django注册的版本不同。我相信他用0.8重写了。