Django update field in many to many relationship

32 views
Skip to first unread message

sum abiut

unread,
Feb 14, 2019, 5:50:21 PM2/14/19
to django...@googlegroups.com
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})

sum abiut

unread,
Feb 14, 2019, 6:22:48 PM2/14/19
to django...@googlegroups.com
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

Simon Charette

unread,
Feb 14, 2019, 6:42:04 PM2/14/19
to Django users
Your DirectorForm model form doesn't include 'Leave_current_balance' in it's Meta.fields
hence no form field is generated for it.

Cheers,
Simon

sum abiut

unread,
Feb 14, 2019, 8:45:05 PM2/14/19
to django...@googlegroups.com
Thanks,
The Leave_current_balance is from Leave_Balance model which why i haven't included it. I am not sure how to fix that.
Sum
--
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...@googlegroups.com.
To post to this group, send email to django...@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/a03bfa6f-f5c8-4f7c-a1de-ba961c4009c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages