通用视图中的 django 动态 Q 对象

发布于 2024-08-20 14:38:00 字数 825 浏览 3 评论 0原文

我希望能够将 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 技术交流群。

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

发布评论

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

评论(2

山川志 2024-08-27 14:38:00

我认为你只能通过用自定义视图/函数包装通用视图来做到这一点。另请参阅此处:

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

屋顶上的小猫咪 2024-08-27 14:38:00

我认为您只是缺少字段名称周围的引号。

    'qobject': Q(status=("%s" % stat)),

I think your just missing the quotes around the field name.

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