Hi,

I have two models many to many relationships, I am trying to update the
field using a form. when a leave is submitted the director is notified by
email. the director can login to the system to approve the leave using the
form. once the leave is approved, I want to adjust the
Leave_current_balance field  which in on the LeaveBalance model

class LeaveBalance(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True,)
    Leave_current_balance= models.FloatField(null=True, blank=True,
default=None)
    Year=models.CharField(max_length=100,default='')
    def __unicode__(self):
             return  self.Year


class NewLeave(models.Model):
        user=models.ForeignKey(User,default='',on_delete=models.CASCADE)
        leave_balance=models.ManyToManyField(Leave_Balance)
        leave=(
        ('annual','annual'),
        ('sick','sick'),

        )

        
Leave_type=models.CharField(max_length=100,choices=leave,blank=False,default='')

        Total_working_days=models.FloatField(null=True,  blank=False)
        DirAuth=(
            ('Pending','Pending'),
            ('Approved','Approved'),
            ('Rejected','Rejected'),
        )

        
Director_Authorization_Status=models.CharField(max_length=100,choices=DirAuth,default='Pending',blank=False)
        Date_Authorized=models.DateField(null=True,blank=False)
        
Authorized_by_Director=models.CharField(max_length=100,default='',blank=False)

        def __unicode__(self):
            return  self.Leave_type




class DirectorForm(forms.ModelForm):
    class Meta:
        model=NewLeave
        
fields=('Director_Authorization_Status','Authorized_by_Director','Date_Authorized',)
        widgets={
            'Date_Authorized':DateInput()
        }



This function throw the error: u'Leave_current_balance'

def unitDirectorForm(request,id):

    if request.method=='POST':

        getstaffid=NewLeave.objects.get(id=id)
        form = DirectorForm(request.POST, instance=getstaffid)
        if form.is_valid():
            getstaffid = form.save(commit=False)
            getstaffid.save()

            total_days = getstaffid.Total_working_days
            current_balance =
getstaffid.user.leave_balance.Leave_current_balance
            diff_balance = current_balance - total_days
            current_balance = diff_balance
            current_balance=form.fields['Leave_current_balance']
            current_balance.save()
            getstaffid.leave_balance.add(current_balance)

            return HttpResponse('You have successfuly Authorise the leave')

    else:
        #getstaffid=NewLeave.objects.get(id=id)
        form=DirectorForm()
        #c_balance=Leave_Balance.objects.get()
        balance_form = leavebbalanceForm()

    return render(request,'managerauthorisedform.html',{'form':form})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPCf-y52gHWwsm%2BMJUx-K51%3DBZAAYVmvQhXtrN3JzkDTY2%2BsJQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to