On 5/28/07, Bram - Smartelectronix <[EMAIL PROTECTED]> wrote: > > Hi there, > > > > Say I have: > > class TaggedItem(models.Model): > """A tag on an item.""" > tag = models.SlugField() > content_type = models.ForeignKey(ContentType) > object_id = models.PositiveIntegerField() > > content_object = models.GenericForeignKey() > > class Meta: > ordering = ["tag"] > > def __str__(self): > return self.tag > > class Book(models.Model): > writer = models.ForeignKey(User) > tags = models.GenericRelation(TaggedItem) > > > How do I get all Tags for books by Dostoevsky ? :-) > > TaggedItem.objects.filter(content_type=ContentType.objects.get_for_model(Book), > content_object__writer="Dostoevsky") > > ...doesn't work... And > > TaggedItem.objects.filter(content_type=ContentType.objects.get_for_model(Book), > object_id__in=[ book.id for book in > Book.objects.filter(writer="Dostoevsky")]) > > is pretty bad. > > > Should I be looking at extra( ) or is there a smarter way to go about this? > > > - bram
I've just implemented something like this in django-tagging, but it feels like a massive hack: Check out the usage_for_model method in http://django-tagging.googlecode.com/svn/trunk/models.py It starts with a basic query template which will retrieve tags and usage counts for a particular model, taking a dictionary of field lookups as an optional argument. If this is provided, some of Django's query generation internals are used to generate required JOIN and WHERE statements to restrict the tags returned to a subset of the model. If anyone reading that immediately thinks "there's a much cleaner way to do that with the DB API, yuo buffoon", I'm all ears! :) Jonathan. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---