Foreign Key don't work for me

12 views
Skip to first unread message

Sandeep kaur

unread,
May 16, 2012, 2:33:32 PM5/16/12
to django-users
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

Jon Paugh

unread,
May 16, 2012, 8:39:37 PM5/16/12
to django...@googlegroups.com
Ah. I believe, the Amount.job_no (i.e. the foreign key) should not be filled with the id of the Client object, but with the actual client object, that is,

job_no = Client.objects.get(cd['job_no'])

That matches the abstract relationship of, "This Client corresponds to this Amount.", then Django takes care of the db details, i.e. finding the pk.

Sandeep kaur

unread,
May 16, 2012, 10:19:27 PM5/16/12
to django...@googlegroups.com
On Thu, May 17, 2012 at 6:09 AM, Jon Paugh <jpa...@gmx.us> wrote:
> Ah. I believe, the Amount.job_no (i.e. the foreign key) should not be filled
> with the id of the Client object, but with the actual client object, that
> is,
>
> job_no = Client.objects.get(cd['job_no'])
>
> That matches the abstract relationship of, "This Client corresponds to this
> Amount.", then Django takes care of the db details, i.e. finding the pk.
>

Changed the views, even then, tasted no success.
I have changed the views to:

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']
job_no = ClientJob.objects.get(cd['job_no'])
p = Amount(job_no=job_no,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))


--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com
Reply all
Reply to author
Forward
0 new messages