让 Django 管理员在发布后记住我的参数

发布于 2024-09-15 07:27:20 字数 769 浏览 5 评论 0原文

我有一个问题困扰了我一段时间。我想使用 django admin 中的日期来查看特定日期之间的条目。 为此,我为此模型定制了changelist.html,并在其中放置了一个表单。当发布时,我重写了这样的查询集方法,

def queryset(self, request):
    qs = super(ModelAdmin, self).queryset(request)

    if request.POST.has_key('date1'):
        return qs.filter(startdate__gte=request.POST['date1']).filter(startdate__lte=request.POST['date2'])               
    return qs

这很好用,但只有一个小问题。例如,如果我选择以任何方式对结果进行排序,则参数会被忘记。

如果我不直接在浏览器中输入网址,那么它看起来像这样

http//localhost/admin/some/model/?startdate__gte=2010-01-01&startdate__lte=2010-12-30

我可以排序之后想要因为他们会像这样粘在一起 http//localhost/admin/some/model/?o=5&ot=asc&startdate__lte=2010-12-30&startdate__gte=2010-01-01

我需要使用filterspec来解决这个问题吗?

多谢多谢!

I have a problems thats been bugging me for a while. I want to use dates in django admin to view entries between certain dates.
To do this I have customized my changelist.html for this model and put a form in there. When posted I override the queryset method like this

def queryset(self, request):
    qs = super(ModelAdmin, self).queryset(request)

    if request.POST.has_key('date1'):
        return qs.filter(startdate__gte=request.POST['date1']).filter(startdate__lte=request.POST['date2'])               
    return qs

This works great but its only one little problem. The parameters are forgotten if I for example choose to sort the result in any way.

If I instead of this type in the url straight into the browser so it looks like this

http//localhost/admin/some/model/?startdate__gte=2010-01-01&startdate__lte=2010-12-30

I can sort however I want to afterwards because they´ll stick just like this
http//localhost/admin/some/model/?o=5&ot=asc&startdate__lte=2010-12-30&startdate__gte=2010-01-01

Do I need to use a filterspec to solve this?

Thanks heaps!

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

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

发布评论

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

评论(1

中二柚 2024-09-22 07:27:20

Django 项目有一个更改请求,要求提供此功能。

它正在等待有人在提交之前为建议的补丁编写测试,因此您可以这样做,也可以下载建议的补丁(靠近页面底部)并使用它。

https://code.djangoproject.com/ticket/6903

There is a change request over at the Django project asking for this functionality.

It's waiting for someone to write tests for the proposed patch before it's committed, so you could either do that or you could download the proposed patch (near the bottom of the page) and use it.

https://code.djangoproject.com/ticket/6903

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