Jay Parlar wrote:
>I'm doing a personal app for myself that tracks academic papers I've
>read. As is the style these days, I can apply multiple tags to each
>paper. My basic model is this:
>
>class Tag(models.Model):
> name = models.CharField(maxlength=200, core=True, unique=True)
>
>class Paper(models.Model):
> title = models.CharField(maxlength=200)
> tags = models.ManyToManyField(Tag)
>
>
>What I'd like to be able to do is get "Related Tags" like they have in
>del.icio.us, ie. for a given Tag, I need a reasonably efficient way to
>find all the other tags that it's ever been used with.
>
>
I would do something like this:
def related_tags(tag_name):
return
Tag.objects.filter(paper__tag__name__exact=tag_name).exclude(name__exact=tag_name).distinct()
Filter expression reads like "all tags that linked to any papers that
linked to this tag". Exclude and distinct should be self-explanatory.
Note that with this Tag architecture the search is constrained to only
specific model (Paper in this case). If you have these same tags linked
to another models you won't have them as "related". But this what one
wants usually anyway.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---