Django-nonrel 按外键排序

发布于 2024-11-14 14:22:33 字数 1380 浏览 1 评论 0原文

有没有一种方法可以在外键上使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2024-11-21 14:22:33

我对此进行猜测 - 您正在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文