For that I have written by own function, put it where is
usage_for_model is located
def usage_for_models(self, Models=[], counts=False,
min_count=None):
"""
@param Model: the list of models to count. Empty list means
all models.
Inspired by usage_for_model, I needed a function with total
usage, not
separated by model.
"""
if min_count is not None: counts = True
filters = ''
tagged_item =
backend.quote_name(self._get_related_model_by_accessor('items')._meta.db_table)
for model in Models:
filters = filters + " %(separator)s %
(tagged_item)s.content_type_id = %(content_type_id)s" % {
'separator': 'OR' if filters else
'',
'tagged_item': tagged_item,
'content_type_id':
ContentType.objects.get_for_model(model).id,
}
filters = filters or 'TRUE'
query = """
SELECT DISTINCT %(tag)s.id, %(tag)s.name%(count_sql)s
FROM
%(tag)s
INNER JOIN %(tagged_item)s
ON %(tag)s.id = %(tagged_item)s.tag_id
%%s
WHERE
%%s
GROUP BY %(tag)s.id, %(tag)s.name
%%s
ORDER BY %(tag)s.name ASC""" % {
'tag': backend.quote_name(self.model._meta.db_table),
'count_sql': counts and (', COUNT(*)') or '',
'tagged_item': tagged_item,
}
extra_joins = ''
min_count_sql = ''
params = []
if min_count is not None:
min_count_sql = 'HAVING COUNT(*) >= %%s'
params.append(min_count)
cursor = connection.cursor()
cursor.execute(query % (extra_joins, filters, min_count_sql),
params)
tags = []
for row in cursor.fetchall():
t = self.model(*row[:2])
if counts:
t.count = row[2]
tags.append(t)
return tags
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---