Django ModelForm保存不使用自定义保存

时间:2014-03-07 07:14:43

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

我有一个名为reserve_form.save()的视图,我在models.py中编写了一个自定义保存,这两个保存不同时工作。每当我提交页面调用时都会发送到models.py中的save()。如何使两者同时工作?以下是我的代码:

views.py:

def reservation(request):
    template='reservation/reservation.html'
    reserve_form=ReservationForm()
    email_to=settings.EMAIL_ADMIN
    site=Site.objects.get_current()
    if request.method=='POST':
        reserve_form=ReservationForm(request.POST)
        if reserve_form.is_valid():
            reserve_form.save()
           messages.success(request,_('Your Request for Reservation is submitted,will 

           contact you shortly'))
        else:
            print "reserve_formreserve_formreserve_formreserve_formreserve_formreserve_form",reserve_form.errors
    return render_to_response(template,{'reserve_form':reserve_form},context_instance=RequestContext(request))

models.py:

def save(self,*args,**kwargs):
        print "innnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"
        site=Site.objects.get_current()
        if self.status=='confirmed':
            print "sucessssssssssssssssssssssssssssssssssssssssssssss"
            name=self.name
            date=self.date
            phone=self.phone
            hour=self.hour
            minute=self.minute
            section=self.section
            civilid=self.civilid
            mail=self.email
            quantity=self.quantity
            template=loader.get_template('reservation/reserve_confirm.html')
            c=Context({'name':name,'quantity':quantity,'date':date,'hour':hour,'minute':minute,'section':section,'civilid':civilid,'phone':phone,'email':mail})
            content=template.render(c)
            email=EmailMessage('[%s]%s'%(site.name,'Reservation Notification'),content,settings.DEFAULT_FROM_EMAIL,[mail],[], headers = {})
            email.content_subtype='html'
            email.send()
            super(Reservation,self).save(*args,**kwargs)
        elif self.status=='cancelled':
            template=loader.get_template('reservation/reserve_cancel.html')
            name=self.name
            mail=self.email
            c=Context({'name':name,'site':site})
            content=template.render(c)
            email=EmailMessage('[%s]%s'%(site.name,'Reservation Notification'),content,settings.DEFAULT_FROM_EMAIL,[mail],[],headers = {})
            email.content_subtype='html'
            email.send()
            super(Reservation,self).save(*args,**kwargs)
        else:
            print "errorrrrrrrrrrrrrrrrrrrrrrrrrrrr"

forms.py:

class ReservationForm(forms.ModelForm):

    class Meta:
        model=Reservation
        fields=['quantity','date','hour','minute','section','name','civilid','phone','email']

    def __init__(self,*args,**kwargs):
        super(ReservationForm,self).__init__(*args,**kwargs)

        self.fields['quantity'].widget.attrs['class'] = 'input2'
        self.fields['date'].widget.attrs['class'] = 'input2'
        self.fields['hour'].widget.attrs['class'] = 'input2'
        self.fields['hour'].widget.attrs['placeholder'] = 'Hours'
        self.fields['minute'].widget.attrs['class'] = 'input2'
        self.fields['minute'].widget.attrs['placeholder'] = 'Minutes'
        self.fields['name'].widget.attrs['class'] = 'input2'
        self.fields['civilid'].widget.attrs['class'] = 'input2'
        self.fields['phone'].widget.attrs['class'] = 'input2'
        self.fields['email'].widget.attrs['class'] = 'input2'

1 个答案:

答案 0 :(得分:1)

如果状态为“已确认”或“已取消”,则您的save()方法仅会成功。但是,状态字段不包含在您的表单中,并且永远不会设置为任何内容:因此保存将始终失败。

您应该确保设置字段,或者(最好)在save方法中包含一个基本案例,无论如何都要进行保存。在任何情况下,验证都不属于保存:它应该采用表单或模型清理方法。