On Mon, Aug 8, 2011 at 11:49 PM, Hayyan Rafiq <hayya...@hotmail.com> wrote:

>  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()
>
>
You would really be better off naming Roll_No in Academics something like
"student_info". (And in general changing your naming conventions to match
Python, but this one field in particular is going to be very confusing if
you name it as you have). In the Django ORM, the field will be an object
that contains the associated Student_Info instance, it will not be a simple
Integer containing the Roll_No of that student. The fact that you have
specified Roll_No as the field to link on (which is unnecessary by the way,
the primary key field will be used by default) is irrelevant.



> 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'
> *
>

GPA isn't an attribute of a Student_Info, it's an Attribute of Academics.
Student_Info has an associated academics object due to the OneToOne field in
Academics, to access it's fields you need to specify that is where the field
is:

obj.academics.GPA

The naming of the field used to access the "backwards" relationship is noted
here:
https://docs.djangoproject.com/en/dev/topics/db/queries/#one-to-one-relationships


>
> 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'
>

E_mail is in the linked Student_Info object:

obj.Roll_No.E_mail

(this is why I mentioned Roll_No is probably not really what you want to
name that field)



> >>> obj.Student_Info.GPA
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> AttributeError: 'Academics' object has no attribute 'Student_Info'
>

GPA is directly in the Academics object:

obj.GPA

Linking two models via a ForeignKey or OneToOne field doesn't make all the
attributes of one directly accessible in the other: you need to specify the
relationship you want to follow in order to get at whatever attribute you
are looking for.

Karen
-- 
http://tracey.org/kmt/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to