2

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.

4
  • Just some more info. Using the normal query works: DataSetPG.objects.filter(tower_code="port525") Commented Mar 8, 2019 at 22:53
  • Just in case, what class are you importing for Q? Commented Mar 8, 2019 at 23:02
  • @17slim yes thats the problem, im importing: mongoengine.queryset.visitor How can i use two Q' from different classes? Commented Mar 8, 2019 at 23:15
  • from mongoengine.queryset.visitor import Q as MongoQ and from django.db.models import Q as DjangoQ Commented Mar 8, 2019 at 23:32

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's it. I was already desperate with this and didn't pay attention to the import. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.