Here is my model: class PlaylistTag(models.Model): playlist = models.ForeignKey(Playlist) tag = models.CharField(max_length=128) tag_count = models.PositiveIntegerField(default=1)
The way this model works is simple. If you add a tag to a playlist, and the tag doesn't exist, the tag_count is 1. If you add the same tag again to that playlist, the tag_count is incremented. If you remove that tag, it is decremented. If it reaches 0, I remove the row entirely. A playlist can have multiple tags and a tag can belong to different playlists. What I want to do is wildcard search for a tag sorted by tag count descending, but instead of returning duplicate playlists, I want to sum the tag_count for each playlist that the search returns. For example: Playlist 1, "foo", 3 Playlist 1, "foobar", 2 A query for "foo" would return both of those rows, but I want to sum them for that playlist so that playlist has a "score" of 5 for that query and only return that playlist once in the querySet. querySet = PlaylistTag.objects.select_related().filter(tag__icontains = query).annotate(score=Sum("tag_count")).order_by("-score") The missing part that I don't understand, is how do I only sum tag_count for distinct playlists that are returned for that query. In plain English, I am searching the PlaylistTag table by tag, and I want to rank the results by which Playlist is most relevant for that search by how many tags matched the query. Does this make sense? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.