I would like to run a query in django using a list. I found a similar post on stack overflow that got me to where I am now, but I can't seem to get my code to work. I think the only difference is that I am using REST.
My code below is throwing the error unsupported operand type(s) for &: 'Build' and 'Build'
What I am expecting is that the query will return all entries where the author_id matches any value in the machinesOwned list.
from .models import Build
import operator
from functools import reduce
class buildStatsAPI(generics.ListCreateAPIView):#for build stats
permission_classes = (permissions.IsAuthenticated,)
serializer_class = buildStatsAPI_serializer
def get_queryset(self):
machinesOwned = [4,5,7,9]
query = reduce(operator.and_, (Build(author_id= item) for item in [machinesOwned]))
return Build.objects.filter(query,deleted = 0,).values()
Buildhere? It looks a bit strange that you query withBuild(..) & Build(..)etc as a filtering object? Should these beQobjects?query = reduce(operator.and_, (Q(author_id= item) for item in [machinesOwned]))and it returns :int() argument must be a string, a bytes-like object or a number, not 'list'