Django-nonrel 按外键排序
有没有一种方法可以在外键上使用“order_by”从 django-nonrel 中的数据库返回项目?
完整详细信息如下:
#Models.py class Post(models.Model): article = models.TextField(help_text='Paste or type HTML in here') pub_date = models.DateField() .... class TagItems(models.Model): title = models.CharField(max_length=200) .... class TagRel(models.Model): the_post = models.ForeignKey('Post') the_tag = models.ForeignKey('Tag')
TagRel 定义了 Post 和 TagItems 类之间的多对多关系。
我想要获取每个标签的文章列表。
#Desire output My tag -my first post -my second post My second tag - my other post - another post
到目前为止一切都很好,因为我使用以下内容来过滤数据:
def tagged_posts(): tag_items = TagItems.objects.all() li =[] for item in tag_items: tag_rel_item = TagRel.objects.filter(the_tag__pk = item.pk) li.append(tag_rel_item) return {'list_of_objects': li}
我使用 db-indexer 在 db-indexes.py 中定义查询的过滤器部分。所有这些工作正常,但我想按发布日期对我的帖子进行排序。
Django 文档告诉我使用:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_tag__pub_date')
但是 django-nonrel 似乎不支持 order_by('the_tag__pub_date') 部分。
以下内容也适用于普通 Django:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_post')
之所以有效,是因为模型中的帖子已按日期排序。
但这似乎在 django-nonrel 中也不起作用。
所以我的问题是如何返回按日期排序的帖子(最新>最旧)?
提前致谢
Is there a way of returning items from a database in django-nonrel, using 'order_by' on a foreignkey?
Full details are as follows:
#Models.py class Post(models.Model): article = models.TextField(help_text='Paste or type HTML in here') pub_date = models.DateField() .... class TagItems(models.Model): title = models.CharField(max_length=200) .... class TagRel(models.Model): the_post = models.ForeignKey('Post') the_tag = models.ForeignKey('Tag')
TagRel defines a ManytoMany relationship between Post and TagItems classes.
I am wanting to get a list of articles for each tag.
#Desire output My tag -my first post -my second post My second tag - my other post - another post
All is good so far, as I use the following to filter the data:
def tagged_posts(): tag_items = TagItems.objects.all() li =[] for item in tag_items: tag_rel_item = TagRel.objects.filter(the_tag__pk = item.pk) li.append(tag_rel_item) return {'list_of_objects': li}
I am using db-indexer to define the filter part of the query in db-indexes.py. All this works fine but I want to order my posts by publication dates.
Django docs tell me to use:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_tag__pub_date')
But the order_by('the_tag__pub_date') part does not appear to be supported by django-nonrel.
The following also works in normal Django:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_post')
This works because the Posts are already sorted by date in the model.
But this also does not appear to work in django-nonrel.
So my question is how do I return my posts ordered by date (latest>oldest)?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此进行猜测 - 您正在使用 ManyToManyField。我相信这是使用 App Engine 数据存储上的 ListProperty 实现的。
请参阅数据存储文档中标有“具有多个值的属性可能会出现令人惊讶的行为”的部分:
http://code.google.com/appengine/docs/python/datastore /queries.html
这很可能就是您的结果显示为未排序的原因。 GAE 本身不支持多对多关系。收到结果后,您可能必须自己对它们进行排序。
I'm taking a guess at this - you're using a ManyToManyField. I believe that's implemented using a ListProperty on App Engine's datastore.
See the section in the datastore documentation labeled "Properties With Multiple Values Can Have Surprising Behaviors":
http://code.google.com/appengine/docs/python/datastore/queries.html
That's most likely why your results appear unsorted. ManyToMany relations aren't supported natively in GAE. You'd probably have to sort them yourself after you get the results back.