2012-01-15 57 views
7

私はdjango-userenaを使用しています。私はUserProfileと呼ばれるモデルを持っています。サインアップフォームに余分なフィールドを追加しました。これらのフィールドは正しく表示されますが、データは保存されません。いくつかのフィールドデータを別のモデル(Business)に保存したい。たとえば、私はcontactbusinessのような2つのフィールドを持っています。私は連絡先フィールドがUserProfileモデルになり、businessフィールドはBusiness Modelに行くことを望む。どんな手掛かり? - ユーザーdjango-userenaフォームに余分なフィールドを追加する

にバインドされていますインスタンスを私はつま先がProfileモデルにそれらを保存したい... Userインスタンスにそれらの値を格納しています:ここで

は私のコード

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

-g

は、フォームのきれいな方法でそれを試してみました:-)ん実現? – Ahsan

答えて

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をチェックしてください。私は[バグがあると思う] http://stackoverflow.com/questions/8818435/many-to-many-field-added-to-the-profile-does-not-work-django-userena) – Kulbir

+2

恐ろしい!これで、ドキュメントを更新するだけです:http://docs.django-userena.org/en/latest/faq.html#how-do-i-add-extra-fields-to-forms –

1

私は上記のテクニックを試してみましたが、うまくいくようです。しかし、私はエラーに遭遇しているし、私のインストールでは保存機能に到達することはありません。 USERENA_WITHOUT_USERNAMES = True 新しいフィールドを含めるためにフォームを補強すると、フィールドが必要になるため保存メソッドには決して行きません。これはフィールドではありませんusing(username)を使用します。私はラインここでビューの作成に見る

: userena_settings.USERENA_WITHOUT_USERNAMESと(signup_form == SignupForm)があれば: signup_form = SignupFormOnlyEmail だから、私はSignupFormOnlyEmail代わりのSignupFormをサブクラス化する例を変更し、それが動作します。したがって、USERENA_WITHOUT_USERNAMESを使用している場合は、登録フォームを変更する場合は別のフォームをサブクラス化してください。

私はこれが(正確に)この質問に答えていない、しかし、それはちょっと

関連する問題