Reverse ForeignKey orm django

hi i am new to django orm , can some body help me i have data models in django .

class userprofile(models.model):
       foreignkey User related_name='fU'
class A(models.model) 
      foreignkey userprofile related_name='fa'
Class B(models.model):
      forign key A related_name="fb"
class C (models.model):
      foreignkey B related_name="fC"
   

so what i am trying to achieve is , with User id i want to get information from all tables from top to bottom hierarchy .All values from all tables .

You can get the related C models wth:

C.objects.filter(Bfk__Afk__userprofilefk_id=my_id)

so the related_name=… [Django-doc] is not relevant here, but the name of the ForeignKey itself.

You can use a similar query for B and A.

Вернуться на верх