Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8: Can't use querysets in query

I'm trying to do this:

wider_circle = # some queryset
friends_you_may_know = list(wider_circle.exclude(user_id__in=user.connections))

But I'm getting this error:

RemovedInDjango19Warning: Passing callable arguments to queryset is deprecated

It worked on Django 1.6 but throws an error on 1.8

Thanks :)

like image 601
Nimo Avatar asked Jul 06 '15 11:07

Nimo


People also ask

Why are QuerySets considered lazy?

This is because a Django QuerySet is a lazy object. It contains all of the information it needs to populate itself from the database, but will not actually do so until the information is needed.

How do you add two sets to a query?

Use union operator for queryset | to take union of two queryset. If both queryset belongs to same model / single model than it is possible to combine querysets by using union operator. One other way to achieve combine operation between two queryset is to use itertools chain function.

How do I make multiple rows in Django?

To create multiple records based on a Django model you can use the built-in bulk_create() method. The advantage of the bulk_create() method is that it creates all entries in a single query, so it's very efficient if you have a list of a dozen or a hundred entries you wish to create.


1 Answers

I'm assuming that connections is a Many to Many on the user model. Which means that user.connections is an instance of the related manager. You should try passing a queryset instance ie:

friends_you_may_know = list(wider_circle.exclude(user_id__in=user.connections.all()))
like image 175
CJ4 Avatar answered Sep 30 '22 18:09

CJ4