views:

202

answers:

2

Hi, 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+)/$', my_views.view, view_customers_info),
)

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.

A: 

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

    'qobject': Q(status=("%s" % stat)),
mountainswhim
+4  A: 

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

Felix Kling
Thanks. That makes total sense, I never thought of doing something like that.
mhost