First time poster to Stack Overflow...
I suspect my answer lies in this solution: 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开发者_Python百科()
....
class Item(models.Model):
categories = models.ManyToManyField(Category)
name = models.CharField(max_length=256)
date_added = models.DateTimeField(default=datetime.datetime.today)
....
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)
精彩评论