2017-03-19 77 views
1

我正在開發推介計劃。這是指朋友加入我們的社區。我有一個名爲邀請的模型。我已經使用了兩個相同的模型。在第一種形式中,用戶必須用他/她的電子郵件地址填寫表格。當他請求邀請時,他/她現在可以提及他/她的朋友。如果被推薦的朋友點擊由他發送的推薦鏈接,則獲得這一點。無法指定「[email protected]」。 「Invitation.friend」必須是「邀請」實例

我的第一種形式,即對於邀請請求工作,但是當請求邀請,如果他/她試圖指朋友灌他的電子郵件地址和他/她的朋友的電子郵件地址後,我得到的

錯誤

無法指定「[email protected]」。 「Invitation.friend」必須是 「邀請」實例。

如何將受引用的電子郵件地址(朋友)保存到數據庫?

這裏是我的models.py

class Invitation(models.Model): 
    email = models.EmailField(unique=True, verbose_name=_("e-mail Address")) 
    friend = models.ForeignKey('self', related_name="referral", null=True, blank=True, on_delete=models.CASCADE) 
    invite_code = models.UUIDField(default=uuid.uuid4, unique=True) 
    points = models.PositiveIntegerField(default=0) 
    invite_accepted = models.BooleanField(verbose_name=_('invite accepted'), default=False) 
    request_approved = models.BooleanField(default=True, verbose_name=_('request accepted')) 

forms.py

class ReferForm(forms.Form): 
    sender_email = forms.EmailField(label=_("Your email"), required=True) 
    receiver_email = forms.EmailField(label=_("To email"), required=True) 

    def save(self, sender_email, receiver_email): 
     print('email', sender_email, receiver_email) 
     new_join, created = Invitation.objects.get_or_create(email=sender_email) 
     print ('new_join is', new_join, created) 
     if created: 
      return True 
     return new_join 

views.py

class ReferInvitation(FormView): 
    template_name = 'refer/refer.html' 
    form_class = ReferForm 

    def form_valid(self, form): 
     sender_email = form.cleaned_data.get('sender_email') 
     receiver_email = form.cleaned_data.get('receiver_email') 
     print ('sender_email', sender_email) 
     print ('friend', receiver_email) 
     refer_instance = form.save(sender_email, receiver_email) 
     print ('refer_instance', refer_instance) 
     refer_instance.friend = receiver_email 
     refer_instance.invite_code = get_invite_code() 
     refer_instance.save() 
     messages.success(self.request, 'You invited {0} successfully'.format(receiver_email)) 
     return HttpResponseRedirect('/') 

一個疑問:發件人郵件已保存到他需要的數據庫邀請。我的方式有效嗎?

UPDATE

就像我已經說過,邀請模型是由兩個形式使用。首先RequestInvitation表單使用它,以便用戶可以請求邀請,並且只有該用戶可以使用引用表單。因此,提交表單時,如果我這樣做Invitation.objects.create(email=sender_email, friend=reciever_email)我得到invitation_invitation.email

unique_constraint錯誤。要處理,我試圖做

def save(self, sender_email, receiver_email): 
    try: 
     invite_instance = Invitation.objects.get(email=sender_email) 
    except: 
     invite_instance = None 
    if invite_instance: 
     invite_instance.friend = receiver_email 
     return invite_instance 
    return Invitation.objects.create(email=sender_email, friend=receiver_email) 

這樣,如果我直接去參考表格並填寫SENDER_EMAIL和reciever_email然後它的作品。如果我在請求邀請並填寫參考表格後去參考表格,那麼朋友字段是空的。

+0

將'refer_instance.friend = receiver_email'改爲'refer_instance.instance.friend = receiver_email' –

+0

@BernardParah我得到'Invitation'對象沒有屬性'instance'錯誤。 – pythonBeginner

+0

我想我也有保存功能的問題。 – pythonBeginner

回答

0

提交表單時,請不要將其保存到數據庫,將其設置爲false,因此表示您有更多的事情要處理。

這是一個工作解決方案,其主要變化是模型上使用的FK。

型號。

class Invitation(models.Model): 
    email = models.EmailField(unique=True, verbose_name=("e-mail Address")) 
    friend = models.EmailField(unique=True, null=True) # this was just because I didn't have any model to tie the FK to' 

形式

from django import forms 
from .models import Invitation 


class ReferForm(forms.Form): 
    sender_email = forms.EmailField(label=("Your email"), required=True) 
    receiver_email = forms.EmailField(label=("To email"), required=True) 

def save(self, sender_email, receiver_email): 
    print('email', sender_email, receiver_email) 
    #add the friend=receiver_email param as part of the argument for saving the form 
    new_join, created = Invitation.objects.get_or_create(email=sender_email, friend=receiver_email) 
    print ('new_join is', new_join, created) 
    if created: 
     return True 
    return new_join 

查看

from django.shortcuts import render 

從者,恕不匯入.models ReferForm 從django.http進口HttpResponseRedirect 從django.views.generic.edit進口FormView控件 進口邀請

class ReferInvitation(FormView): 
    template_name = 'refer.html' 
    form_class = ReferForm 

    def form_valid(self, form): 
     sender_email = form.cleaned_data.get('sender_email') 
     receiver_email = form.cleaned_data.get('receiver_email') 
     print ('refer_instance', refer_instance) 
     #saving and updating in the same view is not allowed in django which is why you were getting the first error 
     # moved the saving of the form to the db down, you can do every other thing before saving 
     form.save(sender_email, receiver_email) 
     return HttpResponseRedirect('/') 
+0

我不使用modelForm,所以我想我得到save()得到了一個意想不到的關鍵字參數'commit'。 – pythonBeginner

+1

對不起,我錯過了你沒有繼承ModelForm ...更新我的答案 –

+0

我試圖實例化邀請對象並保存到數據庫,但不工作 – pythonBeginner

相關問題