In my Django application, I am using forms for the users to enter the
information.
In this, I have linked two tables namely, ClientJob and Amount, by
using foreign key.
Here are the 2 tables:
models.py>
-------------------------------------------------------------------------------------------------------------------------
class ClientJob(models.Model):
"""
:ClientJob:
ClientJob Class is define all field reqiued to submit detail
about new Job.
"""
job_no = models.AutoField(primary_key=True)
receipt_no = models.CharField(max_length=15, editable=False)
type_of_consultancy =
models.CharField(max_length=15,choices=CONSULTANCY_CHOICES)
date = models.DateField(default=datetime.date.today())
site = models.CharField(max_length=600, blank=True )
type_of_work =
models.CharField(max_length=20,choices=ORGANISATION_CHOICES)
letter_no = models.CharField(max_length=100, blank=True,)
letter_date = models.CharField(blank=True, max_length=15 )
file_disposal = models.CharField(max_length=20, choices=MATERIAL_CHOICES)
report_type = models.CharField(max_length=20,choices=REPORT_TYPE)
class ClientJobForm(ModelForm):
class Meta :
model = ClientJob
widgets = {
'name_and_address': TextInput(attrs={'size': 60}),
'site': TextInput(attrs={'size': 60}),
}
class Amount(models.Model):
job_no = models.ForeignKey( ClientJob)
date = models.DateField(default=datetime.date.today(), editable=False)
lab = models.CharField(max_length=100, choices=LAB_CHOICES)
field = models.CharField(max_length=100,choices=FIELD_CHOICES)
other_field = models.CharField(max_length=100,blank=True,null=True)
type = models.CharField(max_length=10, choices=PAYMENT_CHOICES)
def __unicode__(self):
return
self.id()
class AmountForm(ModelForm):
class Meta :
model = Amount
---------------------------------------------------------------------------------------------------------------
and for these fields to be filled I have views as:
views.py>
-------------------------------------------------------------------------------------------------------------
def report_calculation(request):
id = ClientJob.objects.aggregate(Max('job_no'))
maxid =id['job_no__max']
if request.method == 'POST':
form =AmountForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
#job_no = cd['job_no']
type = cd['type'] #sandeep
lab = cd['lab']
field = cd['field']
other_field = cd['other_field']
id = ClientJob.objects.aggregate(Max('job_no'))
client = ClientJob.objects.get(job_no=maxid)
p =
Amount(job_no=client,date=datetime.date.today(),type=type,lab=lab,field=field,other_field=other_field,)
p.save()
return render_to_response('automation/job_ok.html',
{'form': form,
'maxid':maxid}, context_instance=RequestContext(request))
else:
form = AmountForm()
return render_to_response('automation/job_add.html', {'form': form,
'maxid': maxid}, context_instance=RequestContext(request))
----------------------------------------------------------------------------------------------------------------------------------
But unfortunately these fields are not filled in Amount. However when
job_no is not taken as foreign key, everything works fine.
Please help me figure out the error.
--
Sandeep Kaur