what is the use of get_queryset

237 views
Skip to first unread message

Mukul Agrawal

unread,
Jan 25, 2018, 6:31:39 AM1/25/18
to Django users
I have two snap of code below.  I want to know that in the first one I used template_name='restaurants/restaurants_list.html' which is obvious if I want to use a template but in the second one I have not specified the template name even then it shows the correct web page.How? 




class RestaurantListView(ListView):
template_name='restaurants/restaurants_list.html'
def get_queryset(self):
slug=self.kwargs.get("slug")
if slug:
queryset=RestaurantLocation.objects.filter(
Q(category__iexact=slug)|
Q(category__icontains=slug)
)
else:
queryset=RestaurantLocation.objects.all()
return queryset 

##########################################################

class RestaurantListView(ListView):
def get_queryset(self):
return RestaurantLocation.objects.all(owner=self.request.user)




Andréas Kühne

unread,
Jan 25, 2018, 7:32:14 AM1/25/18
to django...@googlegroups.com
Hi,

There are defaults that are used for things like template_name. You should strive to follow the templates, then you don't need to write that much code.

Check out the documentation for ListView:

You can also read what the get_queryset function is for.

Regards,

Andréas

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c29ef43a-ff1e-4008-a2c9-099bf226d5df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages