让 Django 管理员在发布后记住我的参数
我有一个问题困扰了我一段时间。我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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