dlamotte / django-tagging

Automatically exported from code.google.com/p/django-tagging
Other
0 stars 0 forks source link

Tools for dealing with tags across models #41

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I am starting to use your tagging module in a series of projects I am working 
on (I ditched my 
nascent tagging app because yours does most of the stuff that mine did, and 
more stuff that 
mine had not yet gotten to.)

One of the big barriers that I am overcoming by adding code to my projects is 
dealing with cross 
model use of tags.

For example, I have forums which have discussions with have posts in one app. I 
have wiki topics 
in another app. I have user pages elsewhere. I also have blogs.

All of these have tags. As a matter of fact a lot of them will share tags. I 
need common views that 
let you see _all_ of the objects (or all of the models) that have a given tag.

Luckily with the generic relations and the simple object structure I can add 
this code to a 
tagging-support app in my project that lets people view lists of objects that 
match a tag so they 
can find all the relevant discussions, posts, wiki topics, and blog entries 
that cover a specific tag.

Do you have any ideas or plans in this direction? (I am not sure this is an 
"Issue" but I do not see 
any other way to submit comments on google code.)

Original issue reported on code.google.com by scan...@apricot.org on 30 May 2007 at 11:47

GoogleCodeExporter commented 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

GoogleCodeExporter commented 8 years ago
Hm. Thanks. Food for thought. 

Original comment by scan...@apricot.org on 31 May 2007 at 6:15

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
@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