TraceBack:

Request Method: POST
Request URL: http://127.0.0.1:8000/unitDirectorForm/25/

Django Version: 1.11.18
Python Version: 2.7.15
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'mathfilters',
 'profilepage']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File 
"/root./virtualenvs/eleave/local/lib/python2.7/site-packages/django/core/handlers/exception.py"
in inner
  41.             response = get_response(request)

File 
"/root./virtualenvs/eleave/local/lib/python2.7/site-packages/django/core/handlers/base.py"
in _get_response
  187.                 response =
self.process_exception_by_middleware(e, request)

File 
"/root./virtualenvs/eleave/local/lib/python2.7/site-packages/django/core/handlers/base.py"
in _get_response
  185.                 response = wrapped_callback(request,
*callback_args, **callback_kwargs)

File "/var/www/projects/eleave/profilepage/views.py" in unitDirectorForm
  179.                 current_balance=form.fields['Leave_current_balance']

Exception Type: KeyError at /unitDirectorForm/25/
Exception Value: u'Leave_current_balance



On Fri, Feb 15, 2019 at 11:49 AM sum abiut <suab...@gmail.com> wrote:

> 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-y4cTNqkRK0-cwAWMU%3Dq5LXP2%2Bx_Ku9md4SZFT--kDPzmA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to