so i tried this now
class Student_Info(models.Model):
Roll_No = models.IntegerField(primary_key=True)
Cell_No = models.IntegerField()
E_mail = models.EmailField(max_length=30)
class Academics(models.Model):
#Roll_No = models.ForeignKey('Student_Info',to_field='Roll_No')
Roll_No = models.OneToOneField('Student_Info',to_field='Roll_No')
GPA = models.IntegerField()
Then i tried this :
>>> from DbDemo.models import Student_Info
>>> list=Student_Info.objects.all()
>>> obj=list[0]
>>> obj
<Student_Info: Student_Info object>
>>> obj.GPA
>>> obj.GPA
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Student_Info' object has no attribute 'GPA'
Then i tried
>>> from DbDemo.models import Academics
>>> list= Academics.objects.all()
>>> obj=list[0]
>>> obj
<Academics: Academics object>
>>> obj.E_mail
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Academics' object has no attribute 'E_mail'
>>> obj.Student_Info.GPA
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Academics' object has no attribute 'Student_Info'
Any ideas or suggestions??
Date: Mon, 8 Aug 2011 23:07:59 -0400
From: [email protected]
To: [email protected]
Subject: Re: Need help writing join Django query for this simple model..
Make your life a little easier and add a related_name argument to
your Roll_No foreign key.
Example:
Roll_No
= models.ForeignKey('Student_Info',to_field='Roll_No',
related_name = 'students')
Then:
obj.students.all()[0].E_mail
Without a related name:
obj.student_set.all()[0].E_mail
Note:
If it's a one-to-one relationship then you should use a
OneToOne instead of a ForeignKey. That will allow something like
this:
obj.studient.E_mail
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.