如何在django中的另一个查询中获取查询结果

时间:2014-08-27 23:33:17

标签: django django-models django-queryset django-orm

我有以下型号:

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 个答案:

答案 0 :(得分:1)

试试这个

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

详细了解lookups that span relationships