Pedro Faria
Pedro Faria

Reputation: 47

Django cannot unpack non-iterable 'Q' object

Im trying to use Q to make query in django. Database im using for this class is PostgreSQL.

My model is:

class DataSetPG(models.Model):
tower_code = models.CharField(max_length=20, null=False)
time_stamp = models.DateTimeField(default=datetime.now, null=True, blank=True)
value = models.CharField(max_length=200)

class Meta:
    ordering = ('tower_code',)

def __str__(self):
    return "%s" % self.tower_code

My view is asking for:

DataSetPG.objects.filter(Q(tower_code="something"))

But i got this error:

TypeError: cannot unpack non-iterable Q object

What I'm doing wrong? I tried .get instead of .filter and many many other kinds of stuff, but nothing. Im also using Q for querying in mongo database and works fine.

Upvotes: 2

Views: 988

Answers (1)

17slim
17slim

Reputation: 1243

Putting my comment as an answer:

You can't use the Mongo Q as a Django Q to my understanding. You should instead import like so:

from mongoengine.queryset.visitor import Q as MongoQ
from django.db.models import Q as DjangoQ

With this, instead of Q(tower_code='something') use DjangoQ(tower_code='something'). Wherever you used the Q from mongoengine, replace it with MongoQ.

Upvotes: 3

Related Questions