order_by() 不能与 Django 视图中的 filter() 一起使用
我的模型:
...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经完成了很多
.filter().order_by()
链,就像你在那里拥有它们一样,并且没有任何东西跳出来让我觉得不合适。我从来没有尝试过在不进一步处理对象的情况下将该排序传递给模板(通常会迭代它们),所以我想知道order_by()
是否作为 django 惰性求值的一部分丢失了?也许尝试将filter().order_by()
行包装在list()
中以强制在那里进行评估,而不是推迟到稍后的时间?这是一次黑暗中的尝试,但速度足够快,值得一试。
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 theorder_by()
is lost as part of django's lazy evaluation? Maybe try wrapping thefilter().order_by()
line in alist()
to force evaluation there instead of it being put off till some later time?It is a shot in the dark, but quick enough to be worth a try.
您应该尝试按 slot__id 订购。
像这样:
You should try to order by slot__id.
Like this:
如果您也使用 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
在您的views.py中
您不需要使用
(带双下划线)
由于“Album”与 ManyToManyField 不相关,
此处“Album”只是一个外键
,因此 views.py 应该具有
(带有一个下划线)。
参考 django 文档: https:// docs.djangoproject.com/en/2.0/topics/db/queries/#lookups-that-span-relationships
In your views.py
You need not use
(with double underscores)
since 'Album' is not related as a ManyToManyField
here 'Album' is just a ForeignKey
so the views.py should have
(with a single underscore).
reference to django doc: https://docs.djangoproject.com/en/2.0/topics/db/queries/#lookups-that-span-relationships
试试这个,
xyz = Post.objects.filter(do some ).order_by('-slot')
try this,
xyz = Post.objects.filter(do something ).order_by('-slot')