2011-02-25 103 views
5

我有一個名爲「組織」的模型,我已經設置爲用戶配置文件,我希望從「組織」模型的字段顯示在註冊頁面上。我怎麼去做這個與Django的註冊。向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' 

回答

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) 

,然後在基本URL ,重寫現有的URL登記,並添加這種形式爲您的形式使用:

form organization.forms import RegistrationFormWithOrganization 

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

記住Django會使用正則表達式匹配的第一個網址,因此將匹配您的電話,而不是Django的註冊的。它還會告訴註冊使用您的表單,而不是它自己的表單。我在這裏跳過了很多驗證(並且,可能是用戶對象的派生...如果是這樣,請閱讀註冊的源代碼以查看它來自哪裏),但這絕對是正確的軌道在頁面上添加一些東西,儘量減少你的工作量。

+0

這是行不通的,因爲(a)'RegistrationForm'沒有'user'場,所以'self.cleaned_data [ '用戶'] '會拋出一個'KeyError'異常; (b)你需要一個'User'對象來創建一個'Organization'對象,直到你調用'RegistrationForm.save'返回新創建的'user'對象時纔會得到這個對象。請參閱我的答案以獲得其他解決方案。 – 2011-09-28 09:17:15

+0

@SimonKagwi @SimonKagwi我用你的答案,但我不斷收到此錯誤:TypeError at/accounts/register/ register()至少需要2個非關鍵字參數(給出1) – anc1revv 2012-08-22 01:17:43

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']) 

然後在你的根urlconf [但高於包含registration.urls的正則表達式模式並假設正則表達式爲r'^accounts/'] add:

from organization.forms import CustomRegistrationForm 

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

顯然,你也可以create a custom backend,但恕我直言,這是方式更容易。

+0

我不斷收到此錯誤:register()至少需要2個非關鍵字參數(給出1個)。 – anc1revv 2012-08-22 01:08:05

+0

這是由於django註冊的不同版本。我相信他用0.8進行了重寫。 – 2012-12-18 22:50:01

1

修改代碼如下,並再次

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

+0

@ anc1rew試試這個,看看它是否有效 – flexxxit 2012-08-26 15:42:27

+0

修復它,謝謝。 – Jonah 2013-05-13 23:09:01

0

「以前,用於在註冊期間收集數據的形式被預期實現這將創建新的用戶保存()方法嘗試賬戶不再是這種情況;創建賬戶由後臺處理,所以任何自定義邏輯都應該移入後臺自定義的 後臺,或者將監聽器連接到註冊過程中發送的信號。

詳情:

更多信息,可以發現here

相關問題