Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

Django. How to can I access all objects 'through' another through relation

I'm trying to figure out how I can navigate, or dot walk, through my relations..

I have a User class. For an instance of user they are connected to accounts, via an account_permission class. So:

class User(models.Model):
     # this works fine
     accounts =  models.ManyToManyField('Account', through='AccountPermission')

class AccountPermission(models.Model):
     user = models.ForeignKey('User',  on_delete=models.CASCADE, ) 
     account = models.ForeignKey('Account',  on_delete=models.CASCADE, ) 
      
class Account(models.Model):
     pass

class Booking(models.Model):  
     account = models.ForeignKey('Account',  on_delete=models.CASCADE, )

Now what I'm trying to do, is write a method in the User class that will bring me all the booking instances, via all the connected account permissions and then accounts. Kind of a join I guess, but my DB knowledge is not firm enough to figure out if it's a left outer or something.

In RoR from my distant past, I could just chain the throughs, but it doesn't seem to work the same in Django, and I seen to lack the domain language skills to figure it out in google :-(. Any help gratefully recieved.

question from:https://stackoverflow.com/questions/65829743/django-how-to-can-i-access-all-objects-through-another-through-relation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...