通用视图中的 django 动态 Q 对象
我希望能够将 URL 中捕获的变量传递给 Q 对象以获得通用视图。
我创建了一个通用视图,作为 my_views.view 导入,它处理分页、排序、过滤等内容...
我需要使用 Q 对象,因为某些页面需要一些 OR 过滤器。每个页面还将根据不同的字段(和模型)进行过滤(因此是通用视图)。
示例:
view_customers_info = {
"queryset" : Customer.all(),
'qobject': Q(status=stat),
"extra_context" : {
"title" : 'View Customers',
},
'template_name': 'customer/view.html',
}
urlpatterns = patterns('',
url(r'^customer/(?P<stat>\w+)/$', my_views.view, view_customers_info),
)
在此示例中,此行抱怨 stat 不是全局名称:
'qobject': Q(status=stat),
How can I pass the variable returned in the URL to thedictionary view_customers_info?
我不能简单地将 Q 对象移动到通用视图中,因为其他页面将具有如下所示的 Q 对象:
'qobject': (Q(type=type) | Q(status=stat)),
谢谢。
I want to be able to pass a variable caught in the URL to a Q object for a generic view.
I created a generic view which is imported as my_views.view which handles things like pagination, sorting, filtering etc...
I need to use Q objects because for some pages there will need some OR filters. Each page will also be filtering based on different fields (and models) (hence the generic view).
Example:
view_customers_info = {
"queryset" : Customer.all(),
'qobject': Q(status=stat),
"extra_context" : {
"title" : 'View Customers',
},
'template_name': 'customer/view.html',
}
urlpatterns = patterns('',
url(r'^customer/(?P<stat>\w+)/
In this example, this line complains about stat not being a global name:
'qobject': Q(status=stat),
How can I pass the variable caught in the URL to the dictionary view_customers_info?
I can't simply move that Q object into the generic view because other pages will have Q objects like the following:
'qobject': (Q(type=type) | Q(status=stat)),
Thanks.
, my_views.view, view_customers_info),
)
In this example, this line complains about stat not being a global name:
How can I pass the variable caught in the URL to the dictionary view_customers_info?
I can't simply move that Q object into the generic view because other pages will have Q objects like the following:
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你只能通过用自定义视图/函数包装通用视图来做到这一点。另请参阅此处:
http:// /docs.djangoproject.com/en/1.1/topics/generic-views/#complex-filtering-with-wrapper-functions
I think you can only do this by wrapping the generic view with a custom view/function. See also here:
http://docs.djangoproject.com/en/1.1/topics/generic-views/#complex-filtering-with-wrapper-functions
我认为您只是缺少字段名称周围的引号。
I think your just missing the quotes around the field name.