It seems like it would make more sense to do something like:
TaggedItem.objects.filter(book__writer="Dostoevsky")

Unfortunately it looks like this constructs faulty SQL (the join
statement in particular), although it appears like it _should_ work.
Just try TaggedItem.objects.filter(alskdfjladf='dog') and you'll see
that it tells you that books (among other generic relations made by
TaggedItem) is one of the fields it suggests to span across.

Going to post a separate question about this after I try to see if
this topic has been discussed in these groups, but I'd say this is the
cleanest and most intuitive approach.

On May 28, 9:53 am, "Jonathan Buchanan" <[EMAIL PROTECTED]>
wrote:
> 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 
> inhttp://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
-~----------~----~----~----~------~----~------~--~---

Reply via email to