Django 查询按类别中的日期获取下一个上一个项目
第一次在 Stack Overflow 上发帖……
我怀疑我的答案就在这个解决方案中: 从不同类别获取最新对象的 Django 查询 但我在“获取”Django 的 annotate() 功能时遇到问题。
我已经做到了这一点:
previous = Item.objects.filter(date_added__lte=item.date_added).filter(???)[0:1]
我的物品有很多类别。我试图弄清楚如何使用第二个过滤器来测试项目的当前类别(基于 url 参数)是否位于第一个过滤器返回的查询集的 items.categories 中。
可以朝正确的方向推动。
谢谢。 丹·J.
当然。
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)
....
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()
....
class Item(models.Model):
categories = models.ManyToManyField(Category)
name = models.CharField(max_length=256)
date_added = models.DateTimeField(default=datetime.datetime.today)
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我很傻。
没有意识到它与 mtm 字段查找的“categories=category”一样简单。我想我必须检查类别是否在“类别”中。
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.