Re: Foreign Key don't work for me
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.
On Wednesday, May 16, 2012 2:33:32 PM UTC-4, sandy wrote:
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
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/E3aGAQgC9R4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home