order_by() 不能与 Django 视图中的 filter() 一起使用

发布于 2024-11-09 04:00:59 字数 1007 浏览 0 评论 0原文

我的模型:

...
class Bild(models.Model):
    album = models.ForeignKey(Album)
    slot = models.IntegerField()
    bild = models.ImageField(upload_to='bilder', null=True)
    untertitel = models.CharField(max_length=200, null=True)
    def __unicode__(self):

我的观点:

def album_bild(request, album_id, bild_id):
    album_id = int(album_id)
    bilder = Bild.objects.filter(album__id = album_id).order_by('slot')
    ....

当我迭代模板中的“bilder”时,我可以看到 filter() 确实有效,但对象仍然按 pk 而不是“slot”排序。

我使用filter()和order_by()有问题吗?

编辑:我想我应该补充一点,外壳中的一切都工作正常。那么错误可能出在模板中......?

{% for bild in bilder %}
    <li 
    {% ifequal bild.slot bild_id %}
            class="active" 
    {% endifequal %}
    onclick="window.location.href='/portfolio/{{ album_id }}/{{ bild.slot }}'"><div>{{ bild.slot }}</div></li>
{% endfor %}

{% for i in empties %}
    <li class="empty"></li>
{% endfor %}

My model:

...
class Bild(models.Model):
    album = models.ForeignKey(Album)
    slot = models.IntegerField()
    bild = models.ImageField(upload_to='bilder', null=True)
    untertitel = models.CharField(max_length=200, null=True)
    def __unicode__(self):

My view:

def album_bild(request, album_id, bild_id):
    album_id = int(album_id)
    bilder = Bild.objects.filter(album__id = album_id).order_by('slot')
    ....

When I iterate through "bilder" in the template, I can see that the filter() did work but the objects are still ordered by the pk instead of the "slot".

Is there a problem with my usage of filter() and order_by()?

EDIT: I guess i should add that everything works fine in the shell. So maybe the error is in the template...?

{% for bild in bilder %}
    <li 
    {% ifequal bild.slot bild_id %}
            class="active" 
    {% endifequal %}
    onclick="window.location.href='/portfolio/{{ album_id }}/{{ bild.slot }}'"><div>{{ bild.slot }}</div></li>
{% endfor %}

{% for i in empties %}
    <li class="empty"></li>
{% endfor %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

我爱人 2024-11-16 04:00:59

我已经完成了很多 .filter().order_by() 链,就像你在那里拥有它们一样,并且没有任何东西跳出来让我觉得不合适。我从来没有尝试过在不进一步处理对象的情况下将该排序传递给模板(通常会迭代它们),所以我想知道 order_by() 是否作为 django 惰性求值的一部分丢失了?也许尝试将 filter().order_by() 行包装在 list() 中以强制在那里进行评估,而不是推迟到稍后的时间?

bilder = list(Bild.objects.filter(album__id = album_id).order_by('slot'))

这是一次黑暗中的尝试,但速度足够快,值得一试。

I've done lots of .filter().order_by() chains just as you have them there, and nothing jumps out to me as out of place. I've never tried to carry that ordering over to the template without further processing the objects though (usually iterate over them), so I wonder if the order_by() is lost as part of django's lazy evaluation? Maybe try wrapping the filter().order_by() line in a list() to force evaluation there instead of it being put off till some later time?

bilder = list(Bild.objects.filter(album__id = album_id).order_by('slot'))

It is a shot in the dark, but quick enough to be worth a try.

多孤肩上扛 2024-11-16 04:00:59

您应该尝试按 slot__id 订购。

像这样:

bilder = Bild.objects.filter(album__id = album_id).order_by('slot__id')

You should try to order by slot__id.

Like this:

bilder = Bild.objects.filter(album__id = album_id).order_by('slot__id')
姜生凉生 2024-11-16 04:00:59

如果您也使用 Django Rest Framework,我最终会在这里搜索为什么 .order_by() 在应用于 ModelView 中的查询集时不起作用,无论 .filter() 是否存在。

如果您不需要定义自己的 get_queryset,那就不要! django-filters 库为我解决了这个问题,仅使用 OrderFilter 将条件 get_queryset 函数替换为 3 行。

对我有帮助的原始答案

If you too are using Django Rest Framework, I ended up here searching for why .order_by() was not working when applied to a queryset in my ModelView regardless of .filter() being present or not.

If you don't need to define your own get_queryset, don't! The django-filters library solved this issue for me replacing my conditional get_queryset function into 3 lines just using the OrderFilter.

Original answer that helped me

神回复 2024-11-16 04:00:59

在您的views.py
您不需要使用

filter(album__id = album_id)

(带双下划线)
由于“Album”与 ManyToManyField 不相关,

此处“Album”只是一个外键

,因此 views.py 应该具有

def album_bild(request, album_id, bild_id):
    album_id_local = int(album_id)
    bilder = Bild.objects.filter(album_id=album_id_local).order_by('slot')
    ....

(带有一个下划线)。

参考 django 文档: https:// docs.djangoproject.com/en/2.0/topics/db/queries/#lookups-that-span-relationships

In your views.py
You need not use

filter(album__id = album_id)

(with double underscores)
since 'Album' is not related as a ManyToManyField

here 'Album' is just a ForeignKey

so the views.py should have

def album_bild(request, album_id, bild_id):
    album_id_local = int(album_id)
    bilder = Bild.objects.filter(album_id=album_id_local).order_by('slot')
    ....

(with a single underscore).

reference to django doc: https://docs.djangoproject.com/en/2.0/topics/db/queries/#lookups-that-span-relationships

老街孤人 2024-11-16 04:00:59

试试这个,

xyz = Post.objects.filter(do some ).order_by('-slot')

try this,

xyz = Post.objects.filter(do something ).order_by('-slot')

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