Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ListView with subquery gives This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Tags:

django

I have the following model:

class Credit(models.Model):
  user = models.ForeignKey(User)
  ...

and a ListView

class CreditListView(ListView):
  paginate_by = 10
  model = Credit
  ...

if I want to filter the credits by users in side CreditListView:

def get_queryset(self):
    users = User.objects.filter(...)[:10]
    credits = Credits.objects.filter(user__in=users)
    return credits

I will get a NotSupportedError exception:

(1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")
like image 980
James Lin Avatar asked Oct 22 '14 22:10

James Lin


2 Answers

Try this :

users = User.objects.filter(...)[:10]
users = list(users)
credits = Credits.objects.filter(user__in=users)

Django model returns a list which exactly isn't a Python list but an extended form of that. But that has some limitations. Hence, we need to convert that into a Python understandable list.

like image 136
rajan133 Avatar answered Sep 29 '22 08:09

rajan133


The problem was this line:

users = User.objects.filter(...)[:10]

It doesn't like the limit within the subquery, I thought I have tried removing it, could be django server didn't restart properly.

like image 35
James Lin Avatar answered Sep 29 '22 06:09

James Lin