The db-api is constructing a query that uses 'id' as the key for the join, when it should be using the to_field='dfp_id' as specified in the model.
Ad.objects.get(report__date__gte="2007-06-01")
SELECT ...
FROM `campaign_line_item_ads`
INNER JOIN `reports` AS `campaign_line_item_ads__report`
ON `campaign_line_item_ads`.`id` <-- HERE
= `campaign_line_item_ads__report`.`ad_id`
WHERE (`campaign_line_item_ads__report`.`date` >= '2007-06-01')
class Ad(models.Model):
id = models.IntegerField(primary_key=True)
campaign_line_item = models.ForeignKey(CampaignLineItem)
dfp_id = models.IntegerField(null=True, blank=True)
name = models.CharField(blank=True, maxlength=765)
class Meta:
db_table = 'campaign_line_item_ads'
ordering = ['name']
def __str__(self):
return self.name
class Report(models.Model):
id = models.IntegerField(primary_key=True)
date = models.DateField(null=True, blank=True)
ad = models.ForeignKey(Ad, to_field='dfp_id') <-- Here
measure = models.IntegerField(null=True, blank=True)
class Meta:
db_table = 'reports'