Open GoogleCodeExporter opened 8 years ago
I haven't thought about this much in terms of features for django-tagging, but
for a
basic set of all the objects tagged with a given Tag, you can of course use the
'items' relationship back to all its TaggedItems:
test_tag = Tag.objects.get(name='test')
tagged_item_queryset = test_tag.items.all()
Of course, it's pretty inefficient to grab all the generically related content
via
the generic relation, so while I originally write the following utility
function for
use with jellyroll (with the current 108 jellyroll Items in my database,
Item.objects.all() resulted in 190 queries, while
fetch_content_objects(Item.objects.all()) resulted in 17 queries), it - or
something
of its ilk - can be used with any QuerySet of items which define a generic
relation:
from django.contrib.contenttypes.models import ContentType
def fetch_content_objects(item_queryset):
items = list(item_queryset)
# Group object ids by their content type ids
objects_by_ctype = {}
for item in items:
if not objects_by_ctype.has_key(item.content_type_id):
objects_by_ctype[item.content_type_id] = []
objects_by_ctype[item.content_type_id].append(item.object_id)
# Retrieve content objects in bulk
for ctype_id, object_ids in objects_by_ctype.items():
model = ContentType.objects.get(id=ctype_id).model_class()
objects_by_ctype[ctype_id] = model._default_manager.in_bulk(object_ids)
# Place content objects in the appropriate items
for item in items:
item.object = objects_by_ctype[item.content_type_id][item.object_id]
return items
Original comment by jonathan.buchanan
on 31 May 2007 at 1:26
Hm. Thanks. Food for thought.
Original comment by scan...@apricot.org
on 31 May 2007 at 6:15
not sure how this is supposed to work. could you give an example?
i´m having blogentries and image-galleries. i´d like to display all
blogentries/image-galleries associated with a given tag on one site (orderd by
pub_date). is that possible?
thanks.
Original comment by sehmaschine
on 5 Jun 2008 at 10:08
Hah, I wish I'd read this half an hour ago - I just wrote basically the exact
same code as listed in comment #1.
I have a feature request related to tags across models though: it would be
really useful if you could call an
equivalent function to related_for_model that didn't require a model, and hence
find related tags to a specific
tag across ALL models.
Original comment by simon%si...@gtempaccount.com
on 9 Aug 2008 at 10:50
@simonwillisonnet I got here looking for the exact same feature, I wanted to
use the
tag cloud to show tags on all models. after looking at the code, I decided I'm
more
likely to royally screw it up than to get it working.
has anyone made any progress on something like this?
basically just a version of related_for_model / tag_cloud_for_model which works
with
all related objects, regardless of ContentType.
Original comment by andreple...@gmail.com
on 26 May 2009 at 3:51
Original issue reported on code.google.com by
scan...@apricot.org
on 30 May 2007 at 11:47