GenericView: Attributeerror ‘list’ object has no attribute ‘filter’ // 1-second guide 😮💢

So you are casually overriding get_queryset() in a, say, ListView, like this:

def get_queryset(self):
    entries = [x for x in Entry.objects.all() if x.publications]
    return entries

Because you need to sort the Entry table by a reverse M2M relation publications and then BAM!:

Attribute error 'list' object has no attribute 'filter'

Without a trace of the line you made a mistake in…

The problem is that the get_queryset() needs to return a QuerySet (lol). So the error is raised when Django is checking if it can apply basic methods to what get_queryset() returns.

So to transform your generated list to QuerySet do this:

def get_queryset(self):
        def_entries = [x.pk for x in Entry.objects.all() if x.publications]
        entries = Entry.objects.filter(pk__in=def_entries)
        return entries

Merely a lack of concentration. Where is my coffee… ☕💤🧸

References

2 thoughts on “GenericView: Attributeerror ‘list’ object has no attribute ‘filter’ // 1-second guide 😮💢

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.