我正在尝试制作一个模型表单,用于发送电子邮件以更改用户条目上的电子邮件,并且由于某种原因,access_emailchangeauth.user_id may not be NULL
当我单击表单上的提交时它给了我。我不知道为什么。
代码如下
def changeEmail(request):
data = request.POST if request.method == 'POST' else None
if data:
form = ChangeEmailForm(data)
if form.is_valid():
form.save(user=request.user)
else:
a = request.user
a = u.objects.get(username__iexact=a)
form = UserModificationForm(instance=a)
form2 = ChangeEmailForm()
return render('accounts/modify.html',{'form': form, 'form2': form2, 'usr': a})
#forms.py
class ChangeEmailForm(ModelForm):
class Meta:
model = EmailChangeAuth
fields = ['new_email']
def save(self, *args, **kwargs):
user = kwargs.pop('user')
self.user = user
print user
super(ChangeEmailForm, self).save(*args, **kwargs)
#models.py
class EmailChangeAuth(models.Model):
auth_code = UUIDField(auto=True, unique=True)
user = models.ForeignKey(u)
new_email = models.EmailField(max_length=256)
def __unicode__(self):
return unicode(self.auth_code)