Vendel Utto
Vendel Utto

Reputation: 23

ListView ordered by user and date

I have this view:

class IndexLatest(ListView):
    context_object_name = 'latest'
    model = MyModel
    template_name = 'my_app/index.html'

    def get_queryset(self):
        return MyModel.objects.filter(user_id=self.request.user,)

How can I add the order_by argument to the queryset? The model has a date field, and I've tried to add it after the comma but it did not work out!

Upvotes: 0

Views: 32

Answers (1)

Alasdair
Alasdair

Reputation: 308889

You can chain filter() and order_by() calls on a queryset:

def get_queryset(self):
    return MyModel.objects.filter(user_id=self.request.user).order_by('-date')

Upvotes: 1

Related Questions