2012-01-15 85 views
7

我正在使用django-userena。我有一個叫做UserProfile的模型。我在註冊表單中添加了額外的字段。這些字段顯示正確,但數據未保存。我想將一些字段數據保存到另一個模型(Business)中。例如,我有兩個字段,如contactbusiness。我想聯繫領域將去UserProfile模型和business字段將去Business Model。任何線索?謝謝向django-userena表單添加額外的字段

這裏是我的代碼

class SignupFormExtra(SignupForm): 
    address = forms.CharField(label=_(u'Address'),max_length=30,required=False) 
    contact = forms.CharField(label=_(u'Contact'),max_length=30,required=False) 
    business = forms.CharField(label=_(u'Business Name'),max_length=30,required=False) 

    def save(self): 
     """ 
     Override the save method to save the first and last name to the user 
     field. 

     """ 

     user_profile = super(SignupFormExtra, self).save(commit=False) 

     user_profile.address = self.cleaned_data['address'] 
     user_profile.contact = self.cleaned_data['contact'] 
     user_profile.business = self.cleaned_data['business'] 

     user_profile.save() 

     return user_profile 

UPDATE:我上存儲用戶實例這些價值觀......我想腳趾將它們存儲在剖面模型 - 這是用戶綁定一個實例

+0

你嘗試過它在形式上的清潔方法? – Ahsan 2012-01-15 15:52:19

回答

11

Userena的作者。我已經與「no_access」進行了電子郵件通信,但如果其他人遇到同樣的問題,則應該指出解決方案。第一個錯誤是save方法返回一個配置文件。這是不正確的,它返回一個Django User。因此,您首先必須獲取配置文件並對其進行更改。保存配置文件,然後再次返回用戶以保持其與Userena兼容。

對於Business型號,只需將其添加到save方法中。

class SignupFormExtra(SignupForm): 
    address = forms.CharField(label=_(u'Address'),max_length=30,required=False) 
    contact = forms.CharField(label=_(u'Contact'),max_length=30,required=False) 
    business = forms.CharField(label=_(u'Business Name'),max_length=30,required=False) 

    def save(self): 
     """ 
     Override the save method to save the first and last name to the user 
     field. 

     """ 
     # Original save method returns the user 
     user = super(SignupFormExtra, self).save() 

     # Get the profile, the `save` method above creates a profile for each 
     # user because it calls the manager method `create_user`. 
     # See: https://github.com/bread-and-pepper/django-userena/blob/master/userena/managers.py#L65 
     user_profile = user.get_profile() 

     # Be sure that you have validated these fields with `clean_` methods. 
     # Garbage in, garbage out. 
     user_profile.address = self.cleaned_data['address'] 
     user_profile.contact = self.cleaned_data['contact'] 
     user_profile.save() 

     # Business 
     business = self.cleaned_data['business'] 
     business = Business.objects.get_or_create(name=business) 
     business.save() 

     # Return the user, not the profile! 
     return user 

創建表單後,不要忘記在您的urls.py中重寫userena表單。像這樣的事情會這樣做:

url(r'^accounts/signup/$', 
     userena_views.signup, 
     {'signup_form': SignupFormExtra}), 

這應該做的伎倆!祝你好運。

+0

是的,我收到了你的電子郵件。謝謝。你能檢查一下這個LINK嗎?我認爲[有bug] http://stackoverflow.com/questions/8818435/many-to-many-field-added-to-the-profile-does-not-work-django-userena) – Kulbir 2012-01-16 18:40:39

+2

太棒了!現在你所要做的就是更新文檔:) http://docs.django-userena.org/en/latest/faq.html#how-do-i-add-extra-fields-to-forms – 2013-02-26 11:53:40

1

我已經嘗試了上述技巧,它似乎工作。但是,我遇到了一個錯誤,並且從未進入安裝過程中的保存功能。我在我的設置文件中有這樣的設置: USERENA_WITHOUT_USERNAMES = True 因此,當我擴充表單以包含新字段時,我從來沒有進入保存方法,因爲我得到'字段必需',這是我不是使用(用戶名)。

我在視圖創建線路在這裏看到: 如果userena_settings.USERENA_WITHOUT_USERNAMES和(signup_form == SignupForm): signup_form = SignupFormOnlyEmail 於是,我改變了例子子類SignupFormOnlyEmail而不是SignupForm和它的作品。因此,如果您正在使用USERENA_WITHOUT_USERNAMES,那麼如果您想更改註冊表單,則可以使用其他表單子類。

我知道這不回答這個問題(準確),但是,它確實有點:-)

-g

相關問題