鉴于ModelForm,我在做什么?

时间:2018-12-10 16:26:40

标签: forms methods save

似乎无法获取.save()写入数据库

这是模型

class Customer(models.Model):
    email = models.EmailField()
    postcode = models.CharField(max_length=10)
    def __unicode__(self):
    return self.email
    def save(self):
        self.postcode=upper(self.postcode)
        super(Customer, self).save()

这是视图:

class RegisterForm(ModelForm):
    class Meta:
        model = Customer

def register(request):
    form = RegisterForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            postcode = form.cleaned_data['postcode']
            postcode = upper(postcode)
            try:
                user = Customer.objects.all().filter(email=email).filter(postcode=postcode)[:1]
                if user:
                    error = "You have already registered this email address and postcode, please login."
                    return render_to_response("customer/register_form.html", {'error': error, 'form': form, 'content': content,},context_instance=RequestContext(request))
            except Customer.DoesNotExist:
                cust_obj = Customer(email=email, postcode=postcode)
                cust_obj.save()
                return HttpResponseRedirect('/registration-thankyou/')

也尝试过

new_user = form.save(commit=False)
new_user.save()

不保存到表中不会引发任何错误 为任何见识/帮助而欢呼

0 个答案:

没有答案