# view.py
from .models import Foofrom django.views.generic import ListView
class IndexView(ListView): model = Foo
def get_queryset(self): return Foo.objects.all()
# view.py
from .models import Foofrom django.views.generic import ListView
class IndexView(ListView): model = Foo
class MyBlogPosts(generic.ListView):
def get_queryset(self, request):
return self.model.objects.filter(user=self.request.user)
<h2>True ones</h2>{% for p in object_list %} <!-- This would ideally be a set called 'object_list_true' --> <ul> {% if p.true_or_false == True %} <li>{{ p.foo }}</li> {% endif %} </ul>{% endfor %}
<h2>False ones</h2>{% for p in object_list %} <!-- This would ideally be a set called 'object_list_false' --> <ul> {% if p.true_or_false == False %} <li>{{ p.foo }}</li> {% endif %} </ul>{% endfor %}
Hi Richard,
Don't be discouraged to ask questions. It's what the list is for.
Ludovic is right, you'd normally use the regroup tag. But let's say you wanted to do this, then get_queryset isn't the right method.
It should return a queryset, and one only.
On Saturday 25 February 2017 03:42:11 Richard Jackson wrote:
> class MyBlogPosts(generic.ListView):
> def get_queryset(self, request):
My bad: request is not an argument here.
> ...is it possible to return two uniquely identifiable sets - for
> example, if a model has a function which returns True or False, to
> return one set for all the True and one for all the False?
General overview:
The goal of a View is to turn a HTTP request into a HTTP response. A Template provides context to the template,
A ListView uses a Template to do that and is responsible for providing a list of ModelInstances to the template. The get_queryset() method is used to retrieve those instances.
So, the logical place to provide a different grouping of a queryset is in the part where it gets sent to the template:
class BlogRoll(generic.ListView):
def get_context_data(self, **kwargs):
# let ListView do the hard work first
context = super(BlogRoll, self).get_context_data(**kwargs)
# Now make two groups
qs = context['object_list']
group1 = [ obj for obj in qs if obj.boolean_method() is True ]
group2 = [ obj for obj in qs if obj.boolean_method() is False ]
# update the context
context.update(
{ 'group1': group1, 'group2': group2 }
)
# and return it
return context
--
Melvyn Sopacua