2012-06-15 101 views
0

我有兩個型號:Django查詢加入?

class Contact(models.Model): 
    name = models.CharField(max_length=255) 

class Campaign(models.Model): 
    contact = models.ForeignKey(Contact, related_name="campaigns") 
    name = models.CharField(max_length=255) 

如何選擇,在活動表競選有名字=「CAMPAIGNNAME」的所有聯繫人?

回答

1
Contact.objects.filter(campaign__name='CampaignName') 
+0

應該是'campaigns__name'對嗎?從related_name? –

+0

是的,應該有related_name,所以campaigns_name是正確的。 – sunprophit