2014-08-27 190 views
0

我有以下型號:如何獲得查詢結果的另一個查詢在Django

class Activity(models.Model): 
    name = models.CharField(max_length=128) 
    def __unicode__(self): 
     return self.name 

class CustomerActivity(models.Model): 
    customer = models.ForeignKey(Customer) 
    activity = models.ForeignKey(Activity) 
    def __unicode__(self): 
     return self.activity.name 

我有一個過濾器,得到了客戶的所有CustomerActivities:

customer_activities = CustomerActivity.objects.filter(customer=customer) 

我真的需求是基於customer_activities結果的所有Activity對象嗎?

喜歡的東西

activities = Activity.objects.filter(activity_in=customer_activities)??? 

回答

1

試試這個

activities = Activity.objects.filter(customeractivity__customer=customer) 

瞭解更多關於lookups that span relationships

+0

正是我一直在尋找。我也在爲這個部分尋找無處不在。謝謝。 – Atma 2014-08-27 23:47:05

+0

該鏈接目前已被打破,任何人都知道新的位置? – Chayemor 2017-09-02 07:12:25

+1

https://docs.djangoproject.com/en/1.11/topics/db/queries/#lookups-that-span-relationships @chayemor – karthikr 2017-09-02 10:12:47

相關問題