Dear Russell,

Many thanks to you. That works ! 

The following session goes on Django 1.1.1 with no problem

>>> opts=B._meta
>>> fk_field=[f for f in opts.fields if f.get_internal_type() == 
>>> "ForeignKey"][0]
>>> fk_field.rel.to.objects.all() # this successfully get the queryset of A 

Thank you again.

John Wang
2009-12-04

-------------------------------------------------------------
From:      Russell Keith-Magee
Date:      2009-12-04 10:11:16
To:          django-users
Cc:          
Subject:  Re: How can I know the foreign key model at runtime ?

On Fri, Dec 4, 2009 at 10:01 AM, John Wang <john.wang...@gmail.com> wrote:
> Hi all,
>
> let me express my question more simple and easy to understand.As we
> know we can access the manager B through A as the following:
>
>>>> manager_of_b = a.b_set              # get the manager of B
>>>> queryset_b = manager_of_b.all()   # get all instance of B
>
> now how can I access the manager of A through class B?  e,g I want to:
>
>>>> manager_of_a = B.some_api_i_dont_know
>>>> queryset_a = manager_of_a.all()

>>> B.fk.field.rel.to

will effectively return you the class "A", so

>>> B.fk.field.rel.to.all()

will return all instances of A.

You can also get this by inspecting the _meta attribute of B:

>>> B._meta.get_field('fk').rel.to

Yours,
Russ Magee %-)

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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