Sunday, July 5, 2020

Re: Trouble getting kwargs into filter

Hi,

Do you hire contract based python/django freelancer?


I can help you in this and related tasks  


Best,

Karan Sahu


On Monday, July 6, 2020, Clive Bruton <clive@indx.co.uk> wrote:
I am struggling with getting kwarg values into a queryset filter:

class UsersItemsView(ListView):
    template_name = 'testapp/user_item_list.html'

    def get_queryset(self, **kwargs):
        print('kwargs-userlist')
        print(kwargs)
        #return Item.objects.filter(user__username='clive')
        #return Item.objects.filter(user__id=2)
        return Item.objects.filter(user__id=kwargs['userlist'])

    def dispatch(self, *args, **kwargs):
        print('kwargs-userlist')
        print(kwargs['userlist'])
        return super(UsersItemsView, self).dispatch(*args, **kwargs)


On dispatch, I can see the value for "kwargs['userlist']" printed in the terminal.

But, the queryset returns an error:

    Exception Type: KeyError
    Exception Value: 'userlist'

I understand this to mean that the key 'userlist' is not in kwargs, and this is confirmed if I "print(kwargs)" inside the get_queryset

If I use the hard-coded filters, commented-out above ("user__username='clive'", "user__id=2"), I get the result (from the template) that I expect.

So, how do I get the "kwargs['userlist']" passed to the queryset?


Thanks


-- Clive

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5BD32BE1-331F-4AE5-BB8A-2E769D728972%40indx.co.uk.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOWRdhDTT_7XxfOzUiQWv5DuAw8GhXkx-qZUWgd2Y%3DBTTEjSyw%40mail.gmail.com.

No comments:

Post a Comment