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()

Any idea ?


On Dec 3, 6:39 pm, John Wang <[email protected]> wrote:
> Hi all,
>
> Given the following simple models:
>
> from django.db import models
>
> class A(models.Model):
>     att=models.CharField(max_length=256)
>
> class B(models.Model):
>     fk = models.ForeignKey(A)
>
> My question is , if I have a variable of B class, how can I know what
> is the model its 'fk' is refering to at runtime ?
>
> Acctually I want do something like the following:
>
> >>> b = B
> >>> fk = [ f for f in b._meta.fields if f.get_internal_type()=="ForeignKey" 
> >>> ][0] # find the foreign key field at runtime
> >>> queryset_of_a = fk.datamanager_of_A.all()  # I know this is wrong. here i 
> >>> just want to show what i want: get the queryset of A class at runtime 
> >>> from the B side. what is the correct way ?

--

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.


Reply via email to