How about:

Item.objects.filter(categories__watching__user=request.user).distinct()

I haven't tested it, but if it works it would return all items watched
by the user. Then, to display items by category, I would use the
regroup tag [1] in the template.

[1] http://www.djangoproject.com/documentation/templates/#regroup

On Jul 16, 6:51 am, Burr Settles <[EMAIL PROTECTED]> wrote:
> Hey fellow djanglers,
>
> I am trying to construct a view that's a bit complicated, and
> struggling to create the proper queryset. Hopefully some of you have
> done something like this before, or otherwise have some ideas on how
> to tackle it efficiently.
>
> Imagine we have something like these models:
>
> class Category(models.Model):
>         name = models.CharField(max_length=100)
>         slug = models.SlugField(unique=True)
>
> class Watching(models.Model):
>         user = models.ForeignKey(User)
>         category = models.ForeignKey(Category)
>         priority = models.IntegerField()
>
> class Item(models.Model):
>         name = models.CharField(max_length=100)
>         description = models.TextField(blank=True)
>         categories = models.ManyToManyField(Category)
>
> What I want to do is create a view at "http://example.com/watchlist/";
> that will show all *items* belonging to all categories which the
> logged-in user is "watching." So, the queryset needs to be a set of
> Item objects, but needs to follow a conditional chain like this: Item -> 
> Category -> Watching -> User -> "ME." This involves a mix of foreign-
>
> key and many-to-many relationships, since users can be watching
> multiple categories, and items can also belong to multiple categories
> (which can, in turn, have multiple items). It should also deal with
> duplicates in case an item belongs to multiple categories that are all
> being watched by the user. (Hopefully that all makes sense!)
>
> Anyway, this particular kind of queryset construction is a little
> unclear to me from the Django DB API documentation.
>
> Any ideas?
>
> --B
--~--~---------~--~----~------------~-------~--~----~
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