无法分配" abc@gmail.com"。 " Invitation.friend"必须是"邀请"例

时间:2017-03-19 07:44:29

标签: python django django-models django-forms django-views

我正在制定推荐计划。它指的是一位朋友加入我们的社区。我有一个名为邀请的模型。我使用了两个相同型号的表格。在第一种形式中,用户必须使用他/她的电子邮件地址填写表单。当他请求邀请时,他/她现在可以引用他/她的朋友。如果被推荐的朋友点击他发送的推荐链接,将获得该点。

我的第一个表单,即邀请请求正在工作但是在请求邀请后,如果他/她试图引用填写他的电子邮件地址和他/她朋友的电子邮件地址的朋友,我收到错误的

  

无法指定" abc@gmail.com"。 " 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('/')

有一个疑问:发件人电子邮件在请求邀请时已经保存到数据库中。我的方式有效吗?

更新

正如我所说,邀请模型由两种形式使用。首先,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那么它就可以了。如果我在请求邀请后填写推荐表格并填写推荐表格,那么朋友字段为空。

1 个答案:

答案 0 :(得分: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

来自.forms import ReferForm 来自django.http导入HttpResponseRedirect 来自django.views.generic.edit导入FormView 来自.models导入邀请

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('/')