有兩種型號:Django JOIN。選擇數據+ SELECT COUNT
class KeywordCategory(models.Model):
class Meta():
db_table = 'keywords_category'
category_name = models.CharField(max_length = 100)
...
class Keyword(models.Model):
class Meta():
db_table = 'keywords'
keyword_category = models.ForeignKey(KeywordCategory)
keyword_text = models.CharField(max_length = 100)
我需要得到所有的類別和它們的條目數。
SQL語法:
SELECT keywords_category.*, COUNT(keywords.id) as count_keywords
FROM keywords_category
LEFT JOIN keywords ON keywords_category.id=keywords.keyword_category_id
GROUP by keywords_category.id
@ qor1x'.objects.values('keyword_category__category_name')' – madzohan 2014-11-24 11:17:53