user2842009
user2842009

Reputation:

Django ModelForm save not working with custom save

I have a view where is have called reserve_form.save() and I have written a custom save in my models.py, and these two save is not working at same time. Whenever I submit page call is send to save() in models.py. How to make both work at same time? Below is my code:

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'

Upvotes: 1

Views: 468

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

Your save() method only succeeds if status is "confirmed" or "cancelled". However, the status field is not included in your form, and never gets set to anything: so the save will always fail.

You should either ensure that the field is set, or (preferably) include a base case in the save method that does the save anyway. Validation doesn't belong in save, in any case: it should go in the form or model clean methods.

Upvotes: 1

Related Questions