I’m pretty new at Django and wondering what is the difference between defining model vs queryset in a generic view like ListView. Here’s my code example in my urls.py file for the project:
urlpatterns = patterns('',
url(r'^$', ListView.as_view(
model=Person,
context_object_name='people',
template_name='index.html',
)),
)
I’ve also used the same this:
urlpatterns = patterns('',
url(r'^$', ListView.as_view(
queryset=Person.objects.all,
context_object_name='people',
template_name='index.html',
)),
)
And received the same result on my view. I’m assuming there are different things you can do with a queryset?
Using
model=Personorqueryset=Person.objects.allgive the same result.Let’s look at the code. A
ListViewhas the following method:As you can see, it first looks for
self.querysetand, if that does not exist, forself.model. So there are two possibilities to specify a list: you can provide a queryset yourself or you can specify a model class (in which case Django will call theall()method of the default manager, which isobjects).Yes. If you specify a
model, then you get all instances by default. But if you specify aqueryset, you can also call other methods of a model manager, such asPerson.objects.children()which could return only persons withage <= 12.