[Answered ]-QuerySet working in shell but not in Views.py – Django 1.10

2πŸ‘

βœ…

The problem is not the queryset, but standard Python scoping. You need to think about where the value for restaurant is supposed to be coming from.

In your case, it is clearly coming from the URL; assuming you have a URL something like this:

url(r'^restaurant/(?P<restaurant_id>\d+)/employees/$', ...)

named capturing group, that value will be stored in self.kwargs['restaurant_id']. So you should use that in the filter.

Note also that you can use restaurant_id directly as the field, rather than doing a JOIN

So:

queryset = Employee.objects.filter(restaurant_id=self.kwargs['restaurant'])

0πŸ‘

You are not providing restaurant into the get_queryset. Probably it should be like that:

   def get_queryset(self):
        self.restaurant = get_object_or_404(Restaurant, name=self.args[0])
        return Emploee.objects.filter(pk=self.restaurant)

0πŸ‘

try this –

queryset = Employee.objects.filter(restaurant__pk=pk_value)

you should pass pk_value here and restaurant field should be foreign key to other table. You can check by passing pk_value = 1 as you are doing in shell.

hope this helps. for more info check django model docs – https://docs.djangoproject.com/en/1.10/topics/db/models/

πŸ‘€Mangesh

Leave a comment