views:

37

answers:

1

Hello. First time poster to Stack Overflow...

I suspect my answer lies in this solution: http://stackoverflow.com/questions/2074514/django-query-that-get-most-recent-objects-from-different-categories but I am having trouble 'getting' Django's annotate() functionality.

I've gotten this far:

previous = Item.objects.filter(date_added__lte=item.date_added).filter(???)[0:1]

My Items are manytomany with Categories. I'm trying to figure out how to use the second filter to test that the item's current category (based on a url parameter) is in items.categories of the queryset returned by the first filter.

Could use a push in the right direction.

Thanks. Dan J.

Of course.

class Category(models.Model):
    section = models.ForeignKey(Section)
    name = models.CharField(max_length=50)
    slug = models.SlugField()
    ....

class Item(models.Model):
    categories = models.ManyToManyField(Category)
    name = models.CharField(max_length=256)
    date_added = models.DateTimeField(default=datetime.datetime.today)
    ....
A: 

OK, I'm silly. Didn't realize it was as simple as 'categories=category' for mtm field lookup. Thought I had to check if category was 'in' categories.

previous_items = Item.objects.filter(date_added__lt=item.date_added).filter(categories=category)
Daniel Jewett