2013-02-01 59 views
0

我有一個類似模型,看起來像這樣。django加入選擇相關

class categories(models.Model): 
    name=models.CharField(max_length=50,db_index=True) 

class tag_relation(models.Model): 
    category=models.ForeignKey(categories,db_index=True) 
    relation=models.ForeignKey(main_tb,db_index=True,related_name='categoryrelation') 

class main_tb(models.Model): 
    name=charfield 
    img_file=charfield etc.. 
    location=charfield 

現在我需要做的是選擇一個特定的類別,並進一步使用位置進行過濾。

我正在做的是這樣的。

query=tag_relation.objects.filter(category='1orsomeother').selectrelated('categoryrelation').filter(location='india') 

我知道這不是正確的方法,但我該如何做一個聯接和搜索以提供最佳性能。

回答

1

你需要這樣的:

query = tag_relation.objects.filter(category__id=cat_id, relation__location='india')