I faced a similar situation where I had to check for valid input data
used to build the query in get_queryset().  My solution was to use a
decorator around the dispatch() function, but I think it could also be
used within that function.  I chose dispatch() because it acts like a
traditional view function - takes a request and outputs a response.
My code is a little specific to my situation, but it may help. Here is
it:

First, the decorator:

def require_institution():
    """
    Decorator to check for a valid institution id in the session
variables
    and return institution object in kwargs
    """
    def decorator(func):
        def inner(request, *args, **kwargs):
            ## If there is no inst_id set in the session, transfer to
institution select
            ## page for a chance to select one
            try:
                institution =
Institution.objects.get(pk=request.session.get('inst_id',None))
                kwargs['institution'] = institution
            except Institution.DoesNotExist:
                path = urlquote(request.get_full_path())
                tup = reverse('select_inst'), REDIRECT_FIELD_NAME,
path
                return HttpResponseRedirect('%s?%s=%s' % tup)
            return func(request, *args, **kwargs)
        return wraps(func, assigned=available_attrs(func))(inner)
    return decorator

And then the class:

class ListViewInstCheck(ListView):
    model = Event
    paginate_by = DEFAULT_PAGINATION

    @method_decorator(require_institution())
    def dispatch(self, request, *args, **kwargs):
        return super(ListViewInstCheck, self).dispatch(request, *args,
**kwargs)

    def get_queryset(self):
        queryset = super(ListViewInstCheck,self).get_queryset()
        return queryset.filter(institution=self.kwargs['institution'])

Hope this helps.

Dan


On Dec 10, 1:51 pm, Eli Criffield <elicriffi...@gmail.com> wrote:
> So in a class based view inheriting generic.ListView I want to redirect on
> a condition, The logical place to do it is get_queryset, but you can't go
> returning a HttpResponseRedirect from a method that should return a query
> set. The django.shortcuts.redirect() just kinda does that for you so that
> doesn't work either. I can raise a  Http404 from inside get_queryset, but
> not something like raise Redirect('url/').
>
> I saw one answer to the problem where you put the condition
> in render_to_response but that'll gets called after get_queryset. I guess
> the other option is to put the condition in get and check there and return
> redirect. But that seems hacky.
>
> I'm just wonder whats the best way to do this.
>
> Eli Criffield

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to