I have a class :
class ImgObject(models.Model):
img_file = models.CharField(max_length=128,unique=False)
img_id = models.IntegerField(default=0)
img_tags = models.CharField(max_length=320,unique=False)
img_title = models.CharField(max_length=128,unique=False)
Each img_title will have corressponding img_tags. There might be multiple rows of img_title with different set of img_tag. For example,
img_title img_tags
---------------------
buildingA yellow
buildingB seaside
buildingA apartment,urban
buildingC suburban
buildingA yellow
I want my query to return the img_tags of each img_title along with the count of each tag. For example,
SELECT DISTINCT img_title,img_tags, COUNT(img_tags) FROM ImgObject
should return
buildingA : yellow(2),apartment(1),urban(1)
buildingB : seaside
buildingC : suburban
I understand that there should be some iterative process going on, but I am not able to make it into Django query. Here is what I tried,
for i in ImgObject.objects.values('img_title'):
x = ImgObject.objects.filter('img_title').values('img_tags')
y = ImgObject.objects.filter('img_title').values('img_tags').distinct().count()
print x,":",y
But this throws a "ValueError: too many values to unpack" error.
Help me write a code to achieve this output!